Hello, readers! In this article, we will be focusing on Python subprocess module, in detail.
So, let us get started!
What is the subprocess module in Python?
Before understanding about the functionality of subprocess module, let us consider the below situation–
Usually, we write code in Python to automate a process, or to get the results without manual intervention through some UI form or any data-related form. But, what if we need to get the system-level information for some operation or functionality? How do we process system-level script within Python?
This is when
Python subprocess module comes into picture.
With the subprocess module, we can process the system-level scripts within the piece of Python code. It allows us to form a connection with the input. output as well as error pipeline and codes of the system processes.
The subprocess module inculcates various methods to run system level scripts within Python environment:
- subprocess.Popen() and communicate() functions
Let us now have a look at them one by one!
1. The subprocess.call() function
In order to use the functions associated with subprocess module, we need to import the module into the Python environment.
The subprocess.call() function runs the command stated as arguments and returns the value of the code executed successfully or not.
Have a look at the below syntax!
In the below example, we try to execute “echo Hello world” through Python scripting.
For the same, with call() function, the first argument (echo) is considered as the executable command and the arguments after the first arguments are considered as command line arguments.
Further, we need to specify shell = True so that the arguments are treated as string. If set to False, the arguments are treated as path or file paths.
import subprocess print(subprocess.call(["echo" , "Hello world"],shell=True))
As seen below, it returns 0 as the return code. Any other value returned by the code says that the command run isn’t successful.
"Hello world" 0
In this example, we have executed the command “ls -l” through the python script.
import subprocess subprocess.call(["ls","-l"],shell=True)
-rw-r--r-- 1 smulani 1049033 Feb 27 10:40 check.py
2. The subprocess.run() command
As seen above, the call() function just returns the return code of the command executed. It no where helps us have a check on the input and check parameters.
For the same, we have subprocess.run() function that helps us execute the bash or system script within the python code and also returns the return code of the command.
Further, it also returns the arguments passed to the function. By this, it helps us verify the inputs to the system scripts.
import subprocess print(subprocess.run(["echo","Hello World"],shell=True))
"Hello World" CompletedProcess(args=['echo', 'Hello World'], returncode=0)
3. The subprocess.Popen() function
subprocess.Popen() function enables us to execute child programs as a completely new process internally. Further, this can be used to execute shell commands within python.
- stdout: The output value from the command
- stderr: The error returned from the command
Recommended read – Python stdin, stdout, stderr
import subprocess process = subprocess.Popen( ['echo', 'Hello World'],shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE) stdout, stderr = process.communicate() print(stdout)
Here, we have also made use of communicate() function. This function helps us read and fetch the input, output and error values of the script executed from the process directly as shown above.
By this, we have come to the end of this topic. Feel free to comment below, in case you come across any questions. For more such posts related to Python, stay tuned with us.
Till then, Happy Learning!! 🙂