Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I check the operating system in Python?

People also ask

How do you check if os is Windows or Linux in Python?

system() Returns the system/OS name, e.g. 'Linux', 'Windows' or 'Java'. An empty string is returned if the value cannot be determined. If that isn't working, maybe try platform.

What os system does in Python?

Features of the Python OS System The OS system serves as a portable way of interacting with the underlying operating system. It offers access to file names, command line arguments, environment variables, process parameters, and filesystem hierarchy alongside other functionalities.

What is os name Python?

The os.name method in Python get the name of the underlying operating system (OS). This is a method of the OS module. The following are the operating systems that are currently registered.

Which os is used in Python?

The OS module in Python provides functions for interacting with the operating system. OS comes under Python's standard utility modules. This module provides a portable way of using operating system-dependent functionality. The *os* and *os.


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.


You can get a pretty coarse idea of the OS you're 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.

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


More detailed information are available in the platform module.


You can use sys.platform.