Python: Get OS name, platform and release information
Python Basic: Exercise-43 with Solution
Write a Python program to get OS name, platform and release information.
os.name: The name of the operating system dependent module imported. The following names have currently been registered: 'posix', 'nt', 'java'.
platform.system(): Return the name of the OS system is running on.
platform.release(): Return the version of the operating system.
Sample Solution-1:
Python Code:
# Import the 'platform' and 'os' modules.
import platform
import os
# Print the name of the operating system based on the 'os.name' attribute.
# 'os.name' provides the name of the operating system dependent module imported.
print("Name of the operating system:", os.name)
# Print the name of the OS system using the 'platform.system()' function.
# 'platform.system()' returns the name of the operating system, such as 'Windows', 'Linux', or 'Darwin' (macOS).
print("\nName of the OS system:", platform.system())
# Print the version of the operating system using the 'platform.release()' function.
# 'platform.release()' returns the version or release of the operating system.
print("\nVersion of the operating system:", platform.release())
Sample Output:
Name of the operating system: posix Name of the OS system: Linux Version of the operating system: 4.4.0-197-generic
Explanation:
This code imports two modules platform and os in Python.
os.name returns the operating system dependent module imported. The following names have currently been registered: 'posix', 'nt', 'java'.
platform.system() returns the system/OS name, such as 'Linux', 'Darwin', 'Java', 'Windows'. An empty string is returned if the value cannot be determined.
platform.release() returns the system’s release, e.g. '2.2.0' or 'NT'. An empty string is returned if the value cannot be determined.
The code prints the name of the operating system using os.name, the name of the OS system using platform.system() and version of the operating system using platform.release().
The name, version, and type of the operating system on which the code runs will be provided.
Sample Solution-2:
Python Code:
import os
import sys
import platform
import sysconfig
print("os.name ", os.name)
print("sys.platform ", sys.platform)
print("platform.system() ", platform.system())
print("sysconfig.get_platform() ", sysconfig.get_platform())
print("platform.machine() ", platform.machine())
print("platform.architecture() ", platform.architecture())
Sample Output:
os.name posix sys.platform linux platform.system() Linux sysconfig.get_platform() linux-x86_64 platform.machine() x86_64 platform.architecture() ('64bit', 'ELF')
Python Code Editor:
Have another way to solve this solution? Contribute your code (and comments) through Disqus.
Previous: Write a Python program to determine if a Python shell is executing in 32bit or 64bit mode on OS.
Next: Write a Python program to locate Python site-packages.
What is the difficulty level of this exercise?
Test your Programming skills with w3resource's quiz.
It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.
https://w3resource.com/python-exercises/python-basic-exercise-43.php
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics