linux – How do I check the operating system in Python?

linux – How do I check the operating system in Python?

You can use sys.platform:

from sys import platform
if platform == linux or platform == linux2:
    # linux
elif platform == darwin:
    # OS X
elif platform == win32:
    # Windows...

sys.platform has finer granularity than sys.name.

For the valid values, consult the documentation.

See also the answer to “What OS am I running on?”

If you want to know on which platform you are on out of Linux, Windows, or Darwin (Mac), without more precision, you should use:

>>> import platform
>>> platform.system()
Linux  # or Windows/Darwin

The platform.system function uses uname internally.

linux – How do I check the operating system in Python?

You can get a pretty coarse idea of the OS youre using by checking sys.platform.

Once you have that information you can use it to determine if calling something like os.uname() is appropriate to gather more specific information. You could also use something like Python System Information on unix-like OSes, or pywin32 for Windows.

Theres also psutil if you want to do more in-depth inspection without wanting to care about the OS.

Leave a Reply

Your email address will not be published. Required fields are marked *