How to Check the Operating System in Python

How do I check the operating system in Python?

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?”

How to identify on which OS Python is running on?

>>> import os
>>> os.name
'posix'
>>> import platform
>>> platform.system()
'Linux'
>>> platform.release()
'2.6.22-15-generic'

The output of platform.system() is as follows:

  • Linux: Linux
  • Mac: Darwin
  • Windows: Windows

See: platform — Access to underlying platform’s identifying data

Detect OS with python

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'

How can I find the current OS in Python?

I usually use sys.platform to get the platform. sys.platform will distinguish between linux, other unixes, and OS X, while os.name is "posix" for all of them.

For much more detailed information, use the platform module. This has cross-platform functions that will give you information on the machine architecture, OS and OS version, version of Python, etc. Also it has os-specific functions to get things like the particular linux distribution.

How do I check if I'm running on Windows in Python?

Python os module

Specifically for Python 3.6/3.7:

os.name: The name of the operating
system dependent module imported. The
following names have currently been
registered: 'posix', 'nt', 'java'.

In your case, you want to check for 'nt' as os.name output:

import os

if os.name == 'nt':
...

There is also a note on os.name:

See also sys.platform has a finer granularity. os.uname() gives
system-dependent version information.

The platform module provides
detailed checks for the system’s identity.

How can I determine if the operating system a Python script is running on is Unix-like?

The Pythonic way to do it is not to care what platform you are on.

If there are multiple different facilities to accomplish something depending on the platform, then abstract them behind a function or class, which should try a facility and move on to another if that facility is not available on the current platform.

How can I check the operating system from website viewers?

You can not accurately determine the exact OS, but you can get the User-Agent provided by the browser. Flask will parse this into an object in request.user_agent.

from flask import request

request.headers.get('User-Agent')

How to get the system info with Python?

some of these could be obtained from the platform module:

>>> import platform
>>> platform.machine()
'x86'
>>> platform.version()
'5.1.2600'
>>> platform.platform()
'Windows-XP-5.1.2600-SP2'
>>> platform.uname()
('Windows', 'name', 'XP', '5.1.2600', 'x86', 'x86 Family 6 Model 15 Stepping 6, GenuineIntel')
>>> platform.system()
'Windows'
>>> platform.processor()
'x86 Family 6 Model 15 Stepping 6, GenuineIntel'

Is there a way to test the results of an os.system() command in Python?

The return of the os.system function is the return the operating system program (or command) does. Conventionally, it is 0 in case of success and a a number different from 0 if it fails. The error message that you want is not the result of the command, but what is sent to stderr (typically).

Thus, your code should be:

import os
import re

while True:
com = input()

if com == "break":#Doesn't matter
break

ret_value = os.system(com)
if ret_value != 0:
print(f"Error. Command returned {ret_value}")
else:
print("Command returned success")


Related Topics



Leave a reply



Submit