11

I was looking around trying to find a solution to my issue, the best I could find was this:

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

Does anybody know how I could differentiate a Linux PC from android as android Is based off of Linux. And if this is possible, how could I differentiate Mac OS from iOS

kbr85
  • 1,416
  • 12
  • 27
Vortetty
  • 129
  • 1
  • 1
  • 11

4 Answers4

15

Use the platform module:

import platform
print(platform.system())
print(platform.release())
print(platform.version())

Note that a system running on Mac will return 'Darwin' for platform.system()

platform.platform() will return extremely detailed data, such as

'Linux-3.3.0-8.fc16.x86_64-x86_64-with-fedora-16-Verne'
Alec
  • 8,529
  • 8
  • 37
  • 63
11

you can see my github repo https://github.com/sk3pp3r/PyOS and use pyos.py script

import platform 
plt = platform.system()

if plt == "Windows":
    print("Your system is Windows")
    # do x y z
elif plt == "Linux":
    print("Your system is Linux")
    # do x y z
elif plt == "Darwin":
    print("Your system is MacOS")
    # do x y z
else:
    print("Unidentified system")
Haim Cohen
  • 281
  • 3
  • 5
2

From my personal experience, os.uname() has always been a favorite of mine. The uname function only really resides in Linux based systems. Using the function in a method similar to this one, is a good way to detect if you are running a windows system or not:

import os

try:
    test = os.uname()
    if test[0] == "Linux":
        do something here.
execpt AttributeError:
    print("Assuming windows!")
    do some other stuff here.

I hope this helps!

billy
  • 27
  • 8
2

If you want to use just standard libraries, I suggest you to take a look at https://docs.python.org/3/library/sys.html#sys.platform.

Example:

import sys

if sys.platform.startswith('linux'):
    # Linux specific procedures
elif sys.platform.startswith('darwin'):
    # MacOs specific procedures
elif sys.platform.startswith('win32'):
    # Windows specific procedures
Arkar Aung
  • 21
  • 1