Categories
cross-platform platform-specific python

How can I find the current OS in Python? [duplicate]

326

As the title says, how can I find the current operating system in python?

1

  • 5

    Windows: os.name == 'nt'. Mac/Linux/BSD: os.name == 'posix'.

    Jan 4, 2021 at 0:28

339

I usually use sys.platform to get the platform. sys.platform will distinguish between linux, other unixes, and OS X, while os.name is “posix” for all of them.

For much more detailed information, use the platform module. This has cross-platform functions that will give you information on the machine architecture, OS and OS version, version of Python, etc. Also it has os-specific functions to get things like the particular linux distribution.

1

339

I usually use sys.platform to get the platform. sys.platform will distinguish between linux, other unixes, and OS X, while os.name is “posix” for all of them.

For much more detailed information, use the platform module. This has cross-platform functions that will give you information on the machine architecture, OS and OS version, version of Python, etc. Also it has os-specific functions to get things like the particular linux distribution.

1

53

import os
print(os.name)

This gives you the essential information you will usually need. To distinguish between, say, different editions of Windows, you will have to use a platform-specific method.

3

  • 38

    On the mac, os.name gives “posix”, which for my case does not help – sys.platform did the trick

    – Steg

    Jul 27, 2010 at 11:04

  • 3

    in windows it is showing nt only

    Dec 8, 2014 at 12:29

  • 3

    On solaris, it’s showing posix too. :p

    – Khopa

    Mar 31, 2015 at 15:13