In Python, the os module provides various functions to interact with the operating system. One useful function is os.uname(), which allows you to retrieve detailed information about the operating system.
Retrieving System Information
To retrieve the system information using os.uname(), follow these steps:
import os
# Retrieve the system information
system_info = os.uname()
# Print the system information
print(system_info)
The os.uname() function returns a named tuple containing the following system information:
system: The name of the operating system.node: The network name of the machine.release: The system’s release level.version: The system’s version.machine: The machine type.
Example Output
When you run the above code, you will get an output similar to the following:
posix.uname_result(sysname='Linux', nodename='example.com', release='5.4.0-74-generic', version='#83-Ubuntu SMP Sat May 8 02:35:39 UTC 2021', machine='x86_64')
sysname: Linuxnodename: example.comrelease: 5.4.0-74-genericversion: #83-Ubuntu SMP Sat May 8 02:35:39 UTC 2021machine: x86_64
You can access individual components of the returned named tuple by using dot notation. For example, to get just the operating system name, you can do system_info.sysname.
Conclusion
Using os.uname() in Python allows you to easily retrieve important information about the operating system. Whether you need to check the system version or machine type, this function provides a convenient way to access such information within your Python scripts.