kolek kolek - 1 year ago 194
Python Question

How do I check the operating system in Python?

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?

Answer Source


from sys import platform as _platform
if _platform == "linux" or _platform == "linux2":
    # linux
elif _platform == "darwin":
    # OS X
elif _platform == "win32":
    # Windows...
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download