Do you think IIT Guwahati certified course can help you in your career?
No
Introduction
In order to run external programs and commands in our Python script, we need a subprocess. It can create and manage subprocesses which allows us to run these programs.
In this article, we will study the subprocess in Python in depth. We will also study its uses, how to interact with it, and some of its advantages and disadvantages.
What is the Python Subprocess?
In Python, the subprocess is an inbuilt module that provides an interface between creating, controlling, and communicating with other subprocesses. It allows us to execute, compile and access input or output streams in our Python script.
Examples of Subprocess in Python
Some examples of Subprocess in Python are:
Running a Shell Command: You can use subprocess.run() to execute a shell command as if you were running it directly in your terminal. Simply pass the command as a list of strings.
Capturing Output: By specifying stdout=subprocess.PIPE, you can capture the output of a command and use it within your Python script. This is useful for processing or displaying the command's output.
Handling Errors: The subprocess.run() function raises a CalledProcessError if the command returns a non-zero exit status. You can use try-except blocks to handle such errors gracefully.
Running in a Different Directory: You can set the working directory for the command using the cwd parameter. This allows you to run a command in a specific directory.
Redirecting Input: The input parameter allows you to provide input to the command as a string. This can be handy for automating processes that require user input.
Let us see some usage of subprocesses in Python.
Usage of the Python Subprocess
In order to use the subprocess, we first need to import the module
import subprocess
You can also try this code with Online Python Compiler
Now we will see the implementation of some of the functions in the subprocess module.
subprocess.run() function
#importing the module
import subprocess
result = subprocess.run(['echo', 'Greetings of the day Ninja!'], capture_output=True, text=True)
print(result.stdout)
You can also try this code with Online Python Compiler
In the above code, we used the run function to print the statement “Greetings of the day Ninja!” Note that run function used an attribute ‘echo’ which represents that we have to print the string. Therefore, the output of above code will be:
In the above code, we used result.stdout function to print the string. Because the run function stores the string in that variable. If we wrote
print(result)
You can also try this code with Online Python Compiler
In the above code, we used Popen function. It is used to execute a child function in a new process. We used the wait() function in order to wait for the execution of the code.
subprocess.call() function
#importing the module
import subprocess
exit_code = subprocess.call(['ls', '-l'])
print(exit_code)
You can also try this code with Online Python Compiler
The call function is used to run commands with arguments. It will wait for the command to complete or timeout and then return the attribute.
Interacting with subprocess in Python
In Python, there are some techniques to interact with subprocesses. The major techniques are sending input, handling errors, and reading the outputs.
Sending input
We can use the communicate function in order to send the input to a subprocess. Its implementation is as follows:
#importing the module
import subprocess
process = subprocess.popen(['grep', 'example'], stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
input_data = "Hey Ninja! This is a sample input"
stdout, stderr = process.communicate(input_data.encode())
You can also try this code with Online Python Compiler
In the above code, we used the popen() function to reach the child function. Later we used the communicate function to send in the input data.
Handling errors
While execution, we can encounter various errors such as incorrect arguments, commands not found, or a null value exit code. Therefore, it is important to take care of these errors appropriately. We do this with the help of try-except blocks.
#import the module
import subprocess
try:
subprocess.check_call(['ls', 'nonexistent_directory'])
except subprocess.CalledProcessError as e:
print("Error:", e)
You can also try this code with Online Python Compiler
The above code will decode the input and print the string.
Exceptions in Subprocess Module
Below points shows the exceptions in subprocess module.
Calledprocess error: When a subprocess ends but still returns a non-zero status then this error pops out. We can handle this error with the help of the try-catch method.
Timeout expired error: This error occurs when the subprocessing does not complete in a given time limit. It can be handled by setting the a maximum execution time for a subprocess
File not found error: When the command that is executed is not found, then this error pops. This error can be handled by try-catch method.
Advantages of Python subprocess
Integration: With the help of subprocess we can integrate external programs into our Python codes. This expands the working, efficiency, and capabilities of our application.
Platform Independent: With the help of subprocesses, we can interact between multiple platforms. This makes it platform-independent and portable across various operating systems.
Process management: We can handle input and output more accurately. It enables better execution, termination, and handling of the process much easier in a more secure manner.
Disadvantages of Python subprocess
Complexity: One major disadvantage of subprocess is that it can be very complex. When we are dealing with the advanced cases, its complexity is increased.
External dependency: There can be some external dependencies as subprocess works on external programs. It could be dependent on the working, compatibility, and availability of a specific environment.
Most of the time, while using the subprocess the command injection attack occurs. It happens when an attacker is able to inject and change commands. The attacker can have an unauthorised access to the process and can render the code.
Therefore, it is advised to validate the inputs and make sure that they cannor inject the commands.
Frequently asked questions
What is the difference between subprocess call and run?
In Python's subprocess module, call is simpler and older, while run is more flexible and recommended for modern use. run returns a CompletedProcess object, allowing better control and capturing output.
What is the difference between OS and subprocess in Python?
In Python, the os module provides functions to interact with the operating system, while the subprocess module allows you to run external processes and manage their input/output.
What is the difference between subprocess.run() and subprocess.Popen()?
The subprocess.run() function runs a command and waits for it to complete, and capture its output. On the other hand, subprocess.Popen() provides more control over subprocesses. This allows for a real-time interaction and process management.
Can we pass command-line a argument to a subprocess?
Yes, we can pass command-line arguments to a subprocess. It can be done by passing them as elements in a list. With the help of run and Popen function representing the arguments.
Conclusion
In Python, subprocess is an inbuilt module which provides functions to work with the subprocesses. In this article, we studied about the subprocess, its uses, how we can interact with the subprocess, and some advantages and disadvantages of it. We hope you got some basic idea about subprocess in Python. To understand how to run shell commands in Python, you can visit How To Execute Shell Commands With Python?