Python Subprocess and Popen() with Examples – POFTUT

Python Subprocess and Popen() with Examples

Python provides a lot of functionalities with different modules and libraries. When we need to open a new process inside a Python application we can use subprocess library and related functions.

Import subprocess Module

In order to use related functions and libraries, we should import the subprocess library.

Start with call Function

The simples use case to create a subprocess is using call() function. call() function accepts related binary or executable name and parameters as Python list. In this example, we will call Linux ls command with -l and -a parameters.

Start with call Function

Start with call Function

Create Own Shell For Process

While creating a new process the newly created process will use current python shell and associated variables. We can specify the to create a new shell with the shell=True like below.

Create Own Shell For Process

Create Own Shell For Process

Save Process Output (stdout)

After running given command or binary some output may be created. By default, this output is printed to the stdout which is a Python shell for our examples. We can save the process output to a Python variable by calling check_output command like below. In this example, we will save.

Save Process Output (stdout) 

Save Process Output (stdout)

We can see that all created output is saved into the variable named output. When we try to print this variable content to the terminal we will see the exact run command output which is the file list with a user, owner and access time information.

LEARN MORE  How To Redirect Stderr To Stdout In Linux Bash?

Convert Output To A String Instead of Byte Array

In the previous example, we have seen that the output is not a formatted state because the output is saved as a byte array into the output variable. We will provide the output as a string by using universal_newlines option as true for check_output() function.

Convert Output To A String Instead of Byte Array

Convert Output To A String Instead of Byte Array

Get Data From Process

We can communicate with the process by opening the process with Popen() function. We will set the created process instance into a variable and then use communicate() function which will read the process pipe. In this example, we will put hello poftut with the echo command into a pipe and then read with communicate()

Get Data From Process

Get Data From Process

Throw Exception If Run Command Has Error

While running commands with the call() function we may get some error related with the provided command. The error will be printed to the screen like a regular output for the Python.

Throw Exception If Run Command Has Error

Throw Exception If Run Command Has Error

This may be misinterpreted like the command is completed successfully and without error, so the output is printed to the screen. If we want to create some watchdog about command errors we can use check option which will raise an exception if the command has errors.

Throw Exception If Run Command Has Error

Throw Exception If Run Command Has Error

We can see that there is an error

Leave a Reply

Your email address will not be published. Required fields are marked *

Enjoy this blog? Please spread the word :)