Python Subprocess

The subprocess module enables you to start new applications from your Python program. How cool is that?

Related course:
Complete Python Masterclass

Start a process in Python:

You can start a process in Python using the Popen function call. The program below starts the unix program ‘cat’ and the second parameter is the argument. This is equivalent to ‘cat’.  You can start any program with any parameter.

#!/usr/bin/env python 
from subprocess import Popen, PIPE
process = Popen(['cat', ''], stdout=PIPE, stderr=PIPE)
stdout, stderr = process.communicate()
print stdout

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

Subprocess call():

Subprocess has a method call() which can be used to start a program. The parameter is a list of which the first argument must be the program name. The full definition is:, *, stdin=None, stdout=None, stderr=None, shell=False)
# Run the command described by args. 
# Wait for command to complete, then return the returncode attribute.

In the example below the full command would be “ls -l”

#!/usr/bin/env python 
import subprocess["ls", "-l"])

Save process output (stdout)

We can get the output of a program and store it in a string directly using check_output. The method is defined as:

 subprocess.check_output(args, *, stdin=None, stderr=None, shell=False, universal_newlines=False)
 # Run command with arguments and return its output as a byte string.

Example usage:

#!/usr/bin/env python 
import subprocess
s = subprocess.check_output(["echo", "Hello World!"])
print("s = " + s)

Threading and threads

8 thoughts on “Python Subprocess

  1. Hina - January 11, 2016

    Hi Frank,
    i have found your website very useful as i am python learner. please if you could guide me the way to read pdf file in python. Secondly, i want tutorials about natural language processing in python. can you help in this regard.
    Many Thanks

    1. Frank - January 28, 2016

      Hi Hina,
      Python has no standard method to read pdf. To read pdf you need to use a module. You can find related projects here:
      Alternatively you can convert the pdf document to a txt method, using the Pdftotext tool:

  2. Benben - September 1, 2015


    Could tell me why?

    I’ve copy it and run it . it show me:

    from subprocess import Popen, PIPE
    process = Popen(['cat', ''], stdout=PIPE, stderr=PIPE)
    stdout, stderr = process.communicate()
    print stdout
    Traceback (most recent call last):
      File "C:\Python27\learn\", line 5, in 
        process = Popen(['cat', ''], stdout=PIPE, stderr=PIPE)
      File "C:\Python27\lib\", line 710, in __init__
        errread, errwrite)
      File "C:\Python27\lib\", line 958, in _execute_child
    WindowsError: [Error 2]
    1. Frank - September 1, 2015

      The first parameter of Popen() is ‘cat’, this is a unix program.
      I’m not sure if this program is available on windows, try changing it to notepad.exe

      1. Francisco Javier Martin - May 30, 2017

        If you start notepad.exe as a windowed app then python will not get the output.
        The MSDOS command similar to “cat” is “type”.

        process = Popen([‘type’, ‘’], stdout=PIPE, stderr=PIPE)

        1. Frank - May 31, 2017

          Thanks Francisco!

  3. Kheenan - July 16, 2015

    I just want to say These are the best tutorials of any anywhere. I use tutorials all the time and these are just so concise and effective. Thanks a lot and keep at it!

    1. Frank - July 16, 2015

      Thanks Kheenan!