SoFunction
Updated on 2024-11-19

Three ways Python gets the operating system

In this article, we will explain how to get information about your operating system using Python. First, a brief answer to the title:Information about the operating system type, version, host name, CPU architecture, etc. can be easily obtained by using Python built-in modules and third-party libraries.

Type I: Operating System Module

The built-in Python 'platform' and 'sys' modules help us to get information about the operating system.

import platform
import sys

# Get the operating system type
os_type = ()
print("Operating system type:", os_type)

# Get the operating system version number
os_version = ()
print("Operating system version number:", os_version)

# Get the hostname of the operating system
hostname = ()
print("Hostname:", hostname)

# Get the CPU architecture
cpu_architecture = ()
print("CPU Architecture:", cpu_architecture)

# Get the Python version
python_version = sys.version_info
print("Python version:", python_version)

The above code obtains information such as operating system type, version number, hostname, CPU architecture and Python version by calling the appropriate methods.

Second: third-party libraries

In addition to the modules that come with Python, third-party libraries can be used for more detailed and flexible operating system information.

import psutil

# Get all users of the operating system
users = ()
print("Users of all operating systems:", users)

# Get the logical core numbers of the CPU
cpu_count = psutil.cpu_count()
print("Number of logical core CPUs:", cpu_count)

# Get memory usage
memory_usage = psutil.virtual_memory()
print("Memory Usage:", memory_usage)

# Get disk usage
disk_usage = psutil.disk_usage('/')
print("Disk usage:", disk_usage)

The above code uses the third-party library 'psutil in order to get more detailed information about the operating system, including all users, number of CPU cores, memory usage and disk usage.

Third: other operating system information

In addition to the above basic information, information about the operating system can also be obtained by other methods.

For example, it is possible to use Python's 'subprocess to execute system commands via modules that can obtain specific operating system information.

import subprocess

# Get currently logged in users
current_user = subprocess.check_output('whoami').decode("utf-8").strip()
print("Currently logged in users:", current_user)

# Get the boot time of the operating system
uptime = subprocess.check_output('uptime -p').decode("utf-8").strip()
print("Operating system startup time:", uptime)

The above code uses the 'executed using the 'subprocess module' module to execute the 'whoami' and 'uptime -p " commands to get the boot time of the currently logged in user and operating system.

Through the above methods, in order to make appropriate judgment and processing during the development process, we can easily use Python to obtain the relevant information of the operating system.

This article on Python to get the operating system of the three methods of the article is introduced to this, more related to Python to get the operating system content, please search for my previous articles or continue to browse the following related articles I hope that you will support me in the future more!