An Introduction to Subprocess in Python [ Examples Included]

Subprocess in Python is a module used to run new codes and applications by creating new processes. It lets you start new applications right from the Python program you are currently writing. So, if you want to run external programs from a git repository or codes from C or C++ programs, you can use subprocess in Python. You can also get exit codes and input, output, or error pipes using subprocess in Python.

How to Start a Process in Python?

To start a new process, or in other words, a new subprocess in Python, you need to use the Popen function call. It is possible to pass two parameters in the function call. The first parameter is the program you want to start, and the second is the file argument. In the example below, you will use Unix’s cat command and example.py as the two parameters. The cat command is short for ‘concatenate’ and is widely used in Linux and Unix programming. It is like “cat example.py.” You can start any program unless you haven’t created it.

from subprocess import Popen, PIPE

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

stdout, stderr = process.communicate()

print(stdout)

Output:

SubprocessInPython_1

In the above code, the process.communicate() is the primary call that reads all the process’s inputs and outputs. The “stdout” handles the process output, and the “stderr” is for handling any sort of error and is written only if any such error is thrown.

FREE Data Science With Python Course

Start Learning Data Science with Python for FREEStart Learning
FREE Data Science With Python Course

What Is the Subprocess Call()?

Subprocess in Python has a call() method that is used to initiate a program. The syntax of this subprocess call() method is:

subprocess.check_call(args, *, stdin=None, stdout=None, stderr=None, shell=False)

Parameters of Subprocess Call()

The call() method from the subprocess in Python accepts the following parameters:

  • args: It is the command that you want to execute. You can pass multiple commands by separating them with a semicolon (;)
  • stdin: This refers to the standard input stream’s value passed as (os.pipe())
  • stdout: It is the standard output stream’s obtained value
  • stderr: This handles any errors that occurred from the standard error stream
  • shell: It is the boolean parameter that executes the program in a new shell if kept true

Return Value of the Call() Method from Subprocess in Python

The Python subprocess call() function returns the executed code of the program. If there is no program output, the function will return the code that it executed successfully. It may also raise a CalledProcessError exception.

Now, use a simple example to call a subprocess for the built-in Unix command “ls -l.” The ls command lists all the files in a directory, and the -l command lists those directories in an extended format.

import subprocess

subprocess.call(["ls", "-l"])

Output:

SubprocessInPython_2

As simple as that, the output displays the total number of files along with the current date and time.

What is Save Process Output (stdout) in Subprocess in Python?

The save process output or stdout allows you to store the output of a code directly in a string with the help of the check_output function. The syntax of this method is:

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

As you can see, the function accepts the same parameters as the call() method except for one additional parameter, which is:

  • universal_newlines: It is a boolean parameter that opens the files with stdout and stderr in a universal newline when kept true

The method also returns the same value as the call() function. Now, look at a simple example again. This time you will use Linux’s echo command used to print the argument that is passed along with it. You will store the echo command’s output in a string variable and print it using Python’s print function.

import subprocess

s = subprocess.check_output(["echo", "Hello World!"])

print(s)

Output:

SubprocessInPython_3

Free Course: Programming with Python

Learn the Basics of Programming with PythonEnroll Now
Free Course: Programming with Python

Example of Using Subprocess in Python

Let’s combine both the call() and check_output() functions from the subprocess in Python to execute the “Hello World” programs from different programming languages: C, C++, and Java. You will first have to create three separate files named Hello.c, Hello.cpp, and Hello.java to begin. After making these files, you will write the respective programs in these files to execute “Hello World!” Let’s begin with our three files.

Code to Include in Hello.c File

Create a Hello.c file and write the following code in it.

#include<stdio.h>

int main()

{

    printf("C says Hello World!");

    // returning with 0 is essential to call it from Python

    return 0;

}

If you are not familiar with the terms, you can learn the basics of C programming from here.

Code to Write in Hello.cpp File

After the Hello.c, create Hello.cpp file and write the following code in it.

#include <iostream>

using namespace std;

int main()

{

    int a, b;

    cin >> a >> b;

    cout << "C++ says Hello World! Values are:" << a << " " << b;

return 0;

}

Here, this demo has also used integer variables to store some values and pass them through the stdin parameter. If you are not familiar with the terms, you can learn the basics of C++ programming from here.

Code to Write in Hello.java File

Use the following code in your Hello.java file.

class Hello{

public static void main(String args[]){

System.out.print("Java says Hello World!");

}

}

If you are not familiar with the terms, you can learn the basics of Java programming from here.

Python Training Course

Learn Data Operations in PythonExplore Course
Python Training Course

Code to Write in Main.py File to Execute Subprocess in Python

Once you have created these three separate files, you can start using the call() and output() functions from the subprocess in Python. Here’s the code that you need to put in your main.py file.

import subprocess

import os

def C_Execution():

# storing the output

s = subprocess.check_call("gcc Hello.c -o out1;./out1", shell = True)

print(", return code", s)

def Cpp_Execution():

    # creating a pipe to child process

    data, temp = os.pipe()

    # writing inputs to stdin and using utf-8 to convert it to byte string

    os.write(temp, bytes("7 12\n", "utf-8"));

    os.close(temp)

    # storing output as a byte string

    s = subprocess.check_output("g++ Hello.cpp -o out2;./out2", stdin = data, shell = True)

    # decoding to print a normal output

    print(s.decode("utf-8"))

def Java_Execution():

# storing the output

s = subprocess.check_output("javac Hello.java;java Hello", shell = True)

print(s.decode("utf-8"))

# Driver functions

if __name__=="__main__":

C_Execution()

Cpp_Execution()

Java_Execution()

Output:

SubprocessInPython_4

Looking forward to making a move to the programming field? Take up the Python Training Course and begin your career as a professional Python programmer

Summing It Up

In this article, you have learned about subprocess in Python. You can now easily use the subprocess module to run external programs from your Python code. The two primary functions from this module are the call() and output() functions. Once you practice and learn to use these two functions appropriately, you won’t face much trouble creating and using subprocess in Python. 

If you are a newbie, it is better to start from the basics first. You can refer to Simplilearn’s Python Tutorial for Beginners. The tutorial will help clear the basics and get a firm understanding of some Python programming concepts. After the basics, you can also opt for our Online Python Certification Course. The certification course comes with hours of applied and self-paced learning materials to help you excel in Python development.

If you are on the other hand looking for a free course that allows you to explore the fundamentals of Python in a systematic manner - allowing you the freedom to decide whether learning the language is indeed right for you, you could check out our Python for Beginners course or Data Science with Python course. Delivered via SkillUp by Simplilearn, these courses and many others like them have helped countless professionals learn the fundamentals of today’s top technologies, techniques, and methodologies and decide their career path, and drive ahead towards success.

Have any questions for us? Leave them in the comments section of this article. Our experts will get back to you on the same, ASAP!

About the Author

SimplilearnSimplilearn

Simplilearn is one of the world’s leading providers of online training for Digital Marketing, Cloud Computing, Project Management, Data Science, IT, Software Development, and many other emerging technologies.

View More
  • Disclaimer
  • PMP, PMI, PMBOK, CAPM, PgMP, PfMP, ACP, PBA, RMP, SP, and OPM3 are registered marks of the Project Management Institute, Inc.