How to use the subprocess module In Python
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 module import subprocess # traverse the info Id = subprocess.check_output([ 'systeminfo' ]).decode( 'utf-8' ).split( '\n' ) new = [] # arrange the string into clear info for item in Id : new.append( str (item.split( "\r" )[: - 1 ])) for i in new: print (i[ 2 : - 2 ]) |
Output:
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