Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Detect OS with python

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

like image 720
Vortetty Avatar asked May 16 '19 22:05

Vortetty


People also ask

Can Python detect OS?

Detect OS Using the platform Module in Python This method returns information like name, release, and version of the current operating system, name of the machine on the network, and hardware identifier in the form of attributes of a tuple-like object.

What is OS name in 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. RISC OS. Portable Operating System Interface (POSIX)

How do I find my OS in Jupyter notebook?

Use platform. system() . It returns Windows , Linux or Darwin (for OSX).


1 Answers

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'
like image 102
Alec Avatar answered Sep 20 '22 15:09

Alec