Python check operating system

How to Check the Operating System 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?

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:

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).

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 ")
else:
print("Command returned success")

Источник

Читайте также:  Php если папка создана

platform — Access to underlying platform’s identifying data¶

Specific platforms listed alphabetically, with Linux included in the Unix section.

Cross Platform¶

Queries the given executable (defaults to the Python interpreter binary) for various architecture information.

Returns a tuple (bits, linkage) which contain information about the bit architecture and the linkage format used for the executable. Both values are returned as strings.

Values that cannot be determined are returned as given by the parameter presets. If bits is given as » , the sizeof(pointer) (or sizeof(long) on Python version < 1.5.2) is used as indicator for the supported pointer size.

The function relies on the system’s file command to do the actual work. This is available on most if not all Unix platforms and some non-Unix platforms and then only if the executable points to the Python interpreter. Reasonable defaults are used when the above needs are not met.

On macOS (and perhaps other platforms), executable files may be universal files containing multiple architectures.

To get at the “64-bitness” of the current interpreter, it is more reliable to query the sys.maxsize attribute:

is_64bits = sys.maxsize > 2**32 

Returns the machine type, e.g. ‘AMD64’ . An empty string is returned if the value cannot be determined.

Returns the computer’s network name (may not be fully qualified!). An empty string is returned if the value cannot be determined.

platform. platform ( aliased = 0 , terse = 0 ) ¶

Returns a single string identifying the underlying platform with as much useful information as possible.

The output is intended to be human readable rather than machine parseable. It may look different on different platforms and this is intended.

If aliased is true, the function will use aliases for various platforms that report system names which differ from their common names, for example SunOS will be reported as Solaris. The system_alias() function is used to implement this.

Setting terse to true causes the function to return only the absolute minimum information needed to identify the platform.

Changed in version 3.8: On macOS, the function now uses mac_ver() , if it returns a non-empty release string, to get the macOS version rather than the darwin version.

Returns the (real) processor name, e.g. ‘amdk6’ .

An empty string is returned if the value cannot be determined. Note that many platforms do not provide this information or simply return the same value as for machine() . NetBSD does this.

Returns a tuple (buildno, builddate) stating the Python build number and date as strings.

Returns a string identifying the compiler used for compiling Python.

Returns a string identifying the Python implementation SCM branch.

Returns a string identifying the Python implementation. Possible return values are: ‘CPython’, ‘IronPython’, ‘Jython’, ‘PyPy’.

Returns a string identifying the Python implementation SCM revision.

Читайте также:  Cursor execute python psycopg2

Returns the Python version as string ‘major.minor.patchlevel’ .

Note that unlike the Python sys.version , the returned value will always include the patchlevel (it defaults to 0).

Returns the Python version as tuple (major, minor, patchlevel) of strings.

Note that unlike the Python sys.version , the returned value will always include the patchlevel (it defaults to ‘0’ ).

Returns the system’s release, e.g. ‘2.2.0’ or ‘NT’ . An empty string is returned if the value cannot be determined.

Returns the system/OS name, such as ‘Linux’ , ‘Darwin’ , ‘Java’ , ‘Windows’ . An empty string is returned if the value cannot be determined.

platform. system_alias ( system , release , version ) ¶

Returns (system, release, version) aliased to common marketing names used for some systems. It also does some reordering of the information in some cases where it would otherwise cause confusion.

Returns the system’s release version, e.g. ‘#3 on degas’ . An empty string is returned if the value cannot be determined.

Fairly portable uname interface. Returns a namedtuple() containing six attributes: system , node , release , version , machine , and processor .

Note that this adds a sixth attribute ( processor ) not present in the os.uname() result. Also, the attribute names are different for the first two attributes; os.uname() names them sysname and nodename .

Entries which cannot be determined are set to » .

Changed in version 3.3: Result changed from a tuple to a namedtuple() .

Java Platform¶

Version interface for Jython.

Returns a tuple (release, vendor, vminfo, osinfo) with vminfo being a tuple (vm_name, vm_release, vm_vendor) and osinfo being a tuple (os_name, os_version, os_arch) . Values which cannot be determined are set to the defaults given as parameters (which all default to » ).

Windows Platform¶

Get additional version information from the Windows Registry and return a tuple (release, version, csd, ptype) referring to OS release, version number, CSD level (service pack) and OS type (multi/single processor). Values which cannot be determined are set to the defaults given as parameters (which all default to an empty string).

As a hint: ptype is ‘Uniprocessor Free’ on single processor NT machines and ‘Multiprocessor Free’ on multi processor machines. The ‘Free’ refers to the OS version being free of debugging code. It could also state ‘Checked’ which means the OS version uses debugging code, i.e. code that checks arguments, ranges, etc.

Returns a string representing the current Windows edition, or None if the value cannot be determined. Possible values include but are not limited to ‘Enterprise’ , ‘IoTUAP’ , ‘ServerStandard’ , and ‘nanoserver’ .

Return True if the Windows edition returned by win32_edition() is recognized as an IoT edition.

macOS Platform¶

Get macOS version information and return it as tuple (release, versioninfo, machine) with versioninfo being a tuple (version, dev_stage, non_release_version) .

Entries which cannot be determined are set to » . All tuple entries are strings.

Unix Platforms¶

Tries to determine the libc version against which the file executable (defaults to the Python interpreter) is linked. Returns a tuple of strings (lib, version) which default to the given parameters in case the lookup fails.

Читайте также:  font-style

Note that this function has intimate knowledge of how different libc versions add symbols to the executable is probably only usable for executables compiled using gcc.

The file is read and scanned in chunks of chunksize bytes.

Linux Platforms¶

Get operating system identification from os-release file and return it as a dict. The os-release file is a freedesktop.org standard and is available in most Linux distributions. A noticeable exception is Android and Android-based distributions.

Raises OSError or subclass when neither /etc/os-release nor /usr/lib/os-release can be read.

On success, the function returns a dictionary where keys and values are strings. Values have their special characters like » and $ unquoted. The fields NAME , ID , and PRETTY_NAME are always defined according to the standard. All other fields are optional. Vendors may include additional fields.

Note that fields like NAME , VERSION , and VARIANT are strings suitable for presentation to users. Programs should use fields like ID , ID_LIKE , VERSION_ID , or VARIANT_ID to identify Linux distributions.

def get_like_distro(): info = platform.freedesktop_os_release() ids = [info["ID"]] if "ID_LIKE" in info: # ids are space separated and ordered by precedence ids.extend(info["ID_LIKE"].split()) return ids 

Источник

How to Check Operating System Using Python

In this tutorial, you will learn how to determine the current operating system where the python script is running using various methods.

How to Check Operating System Using Python

Python Detect OS — Platform Module

The platform module in Python is part of the standard library. It contains information about the underlying system hardware. We can use the various methods from this module to determine the OS.

Example code is as demonstrated:

import platform os = platform.system() print("Current OS: ", os) 

In the code above, we start by importing the platform module. We then call the system() function from the module which gives us the OS name.

If you are running the script on Windows and MacOS, you will get an output as shown, respectively.

'Windows' [for Windows OS] 'Darwin' [for mac OS] 

Python Detect OS — SYS Module

You are probably familiar with the sys module in Python. This module provides ways of interacting with the running operating system including reading files, environment variables, user information, os information and more.

We can use this module to determine the current OS as shown in the code below:

import sys os = sys.platform() print("Current OS: ", os) 

For other systems, respective output include:

'win32' -> [Windows: win32] 'darwin' -> [mac OS] 

Conclusion

In this tutorial, you came across two simple yet useful methods of determining the current OS using Python. There is not much difference between the two methods, hence use the ones that comes naturally.

If you enjoy our content, please consider buying us a coffee to support our work:

A highly-skilled software engineer with a passion for teaching and sharing knowledge. From development to hardware, get articles with high technical accuracy, clarity, and an engaging writing style.

Источник

Оцените статью