Table of contents
1.
Introduction
2.
What is the Python Subprocess?
3.
Examples of Subprocess in Python
4.
Usage of the Python Subprocess
4.1.
subprocess.run() function
4.2.
subprocess.Popen() function 
4.3.
subprocess.call() function
5.
Interacting with subprocess in Python
5.1.
Sending input
5.2.
Handling errors
5.3.
Reading output
6.
Exceptions in Subprocess Module
7.
Advantages of Python subprocess 
8.
Disadvantages of Python subprocess 
9.
Security Considerations
10.
Frequently asked questions
10.1.
What is the difference between subprocess call and run?
10.2.
What is the difference between OS and subprocess in Python?
10.3.
What is the difference between subprocess.run() and subprocess.Popen()?
10.4.
Can we pass command-line a argument to a subprocess?
11.
Conclusion
Last Updated: Sep 1, 2024
Easy

Introduction to Subprocess in Python

Author Tashmit
0 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

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. 

What is Python Subprocess

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
Run Code


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
Run Code


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:

Output

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
Run Code

It would’ve generated an output like:

Output

As you can see the greetings string is stored in stdout variable.

subprocess.Popen() function 

#importing the module
import subprocess

process = subprocess.popen(['ls', '-l'])
process.wait()
You can also try this code with Online Python Compiler
Run Code


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
Run Code


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
Run Code


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
Run Code


The above code will generate an error statement of non-existing directory as:

Output

Reading output

After successful execution, we want the output result. We can use the decode function to generate the output. 

#importing the module
import subprocess

process = subprocess.Popen(['grep', 'sample'], 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())

# Retrieve and print the output stream (stdout)
output = stdout.decode()
print("Subprocess Output:")
print(output)
You can also try this code with Online Python Compiler
Run Code


The above code will decode the input and print the string.

Output

Exceptions in Subprocess Module

Below points shows the exceptions in subprocess module.

  1. 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. 
     
  2. 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
     
  3. 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.

Also see,  Convert String to List Python

Security Considerations

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?

You may refer to our Guided Path on Coding Ninjas Studio for enhancing your skill set on DSACompetitive ProgrammingSystem Design, etc. Check out essential interview questions, practice our available mock tests, look at the interview bundle for interview preparations, and so much more!

Happy learning, Ninja!

Live masterclass