Python subprocess: The Complete Guide

In the official python docs, we can read that the subprocess should be used for accessing system commands. For example, the subprocess module enables us to spawn processes, connect to their input/output/error pipes, and receive return codes.

Python subprocess

Python subprocess module allows you to spawn new processes, connect to their input/output/error pipes, and obtain their return codes. 

Python subprocess is a built-in module that enables you to start your Python program. In addition, the subprocess module provides a consistent interface for creating and working with additional processes.

The suggested approach to invoking subprocesses is to use the run() function for all use cases it can handle. The underlying Popen interface can be used directly for more advanced use cases.

The run() function was added in Python 3.5; if you need to preserve compatibility with older versions, see the Older high-level API section.

It offers a higher-level interface than some of the other available modules and is intended to replace functions such as os.system()os.spawn*()os.popen*()popen2.*(), and commands.*()

To make it easier to compare the subprocess with those other modules, many examples here re-create the ones used for os and popen.

How to start a process in Python

To start a process in Python, use the subprocess.Popen() function.The program below starts the UNIX program “cat”, and the second parameter is an argument.

It is equivalent to ‘cat’.  You can start the program with any parameter.

Now, inside your python project folder, create two files.


Inside the, write the following code.


student = {
  'name': 'Krunal',
  'enno': 21,
  'college': 'vvp college'

Now, write the following code inside the file.


from subprocess import Popen, PIPE

process = Popen(['cat', ''], stdout=PIPE, stderr=PIPE)
stdout, stderr = process.communicate()

See the below output.

Python Subprocess Tutorial

The process.communicate() call reads the input and output from a process.  The stdout is a process output. The stderr will be written only if the error occurs.  If you want to wait for a program to finish, you can call Popen.wait() function.

Subprocess call() is a built-in function that can be used to start a program. The call() function accepts a list of which the first argument must be the program name.

Syntax, *, stdin=None, stdout=None, stderr=None, shell=False)

See the following code example.

Subprocess call() method

It lists all the files and folders in the current directory.

Save process output (stdout)

We can get the program’s output and store it in the string directly using check_output. The method is defined as the following.

subprocess.check_output(args, *, stdin=None, stderr=None, shell=False, universal_newlines=False)

See the following example.


import subprocess

s = subprocess.check_output(["echo", "AppDividend"])
print("s = " + s.decode('utf-8'))

See the below output.

Save process output (stdout)

Setting a shell argument to the true value causes the subprocess to spawn the intermediate shell process and tell it to run a command. The default is to run a command directly.


import subprocess

# Command with shell expansion'echo $HOME', shell=True)

See the output.

Python Subprocess Tutorial With Example

Error Handling in Subprocess

The return value from the call() function is the program’s exit code. The caller is responsible for interpreting it to detect errors.

The check_call() function works like the call() function except that the exit code is checked, and if it indicates the error happened, then the CalledProcessError exception is raised.

See the following code.


import subprocess


See the output.

Error Handling in Subprocess

That’s it for this tutorial.

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.