Get Your System Information – Using Python Script
Getting system information for your system can easily be done by the operating system in use, Ubuntu let’s say. But won’t it be fun to get this System information using Python script? In this article, we will look into various ways to derive your system information using Python.
There are two ways to get information:
- Using Platform module
- subprocess
1. Using Platform module:
Installation of the platform module can be done using the below command:
pip install platform
Example:
Python3
import platform
my_system = platform.uname()
print (f "System: {my_system.system}" )
print (f "Node Name: {my_system.node}" )
print (f "Release: {my_system.release}" )
print (f "Version: {my_system.version}" )
print (f "Machine: {my_system.machine}" )
print (f "Processor: {my_system.processor}" )
|
Output:
System: Windows
Node Name: LAPTOP-PRKUI1Q9
Release: 10
Version: 10.0.18362
Machine: AMD64
Processor: Intel64 Family 6 Model 78 Stepping 3, GenuineIntel
Using WMI module (only for Windows):
The WMI module can be used to gain system information of a windows machine and can be installed using the below command:
pip install wmi
Example:
Python3
import wmi
c = wmi.WMI()
my_system = c.Win32_ComputerSystem()[ 0 ]
print (f "Manufacturer: {my_system.Manufacturer}" )
print (f "Model: {my_system. Model}" )
print (f "Name: {my_system.Name}" )
print (f "NumberOfProcessors: {my_system.NumberOfProcessors}" )
print (f "SystemType: {my_system.SystemType}" )
print (f "SystemFamily: {my_system.SystemFamily}" )
|
Output:
Manufacturer: LENOVO
Model: 80XH
Name: LAPTOP-PRKUI1Q9
NumberOfProcessors: 1
SystemType: x64-based PC
SystemFamily: ideapad 320-15ISK
Using os module (only for Unix):
Example:
Python3
import os
print (os.uname())
|
Output:
(‘Linux’, ‘mycomputer.domain.user’, ‘2.6.18-92.1.22.el5PAE’, ‘#1 SMP Tue APR 16 12:36:25 EST 2020’, ‘i686’)
Using psutil module:
This is primarily used for getting runtime process information on the system.
Installation of psutil module can be done using the below command:
pip install psutil
Example:
Python3
import psutil
print (f "Memory :{psutil.virtual_memory()}" )
|
Output:
Memory :svmem(total=8458571776, available=2982494208, percent=64.7, used=5476077568, free=2982494208)
2. Using the subprocess module:
We will use the subprocess module to interact with cmd and to retrieve information into your python ide. we can read the cmd command through the subprocess module. It is an inbuilt module in python
Let’s see my logic, if we run this systeminfo code into our terminal then we got like this:
Let’s write python code to get information:
Approach:
- import module
- Get the output for the command “systeminfo” using subprocess.check_output()
- Decode the output with utf-8 split the meta data according to the line
- Now get the Split the string and arrange your data with your own needs.
Implementation:
Python3
import subprocess
Id = subprocess.check_output([ 'systeminfo' ]).decode( 'utf-8' ).split( '\n' )
new = []
for item in Id :
new.append( str (item.split( "\r" )[: - 1 ]))
for i in new:
print (i[ 2 : - 2 ])
|
Output:
Last Updated :
25 Sep, 2020
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...