I want to check the operating system (on the computer where the script runs).
I know I can use os.system('uname -o')
in Linux, but it gives me a message in the console, and I want to write to a variable.
It will be okay if the script can tell if it is Mac, Windows or Linux. How can I check it?
This question is related to
python
linux
operating-system
You can use sys.platform
.
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.
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.
Source: Stackoverflow.com