Subprocess
A subprocess in Python is a task that a python script delegates to the Operative system (OS).
The subprocess library allows us to execute and manage subprocesses directly from Python. That involves working with the standard input stdin
, standard output stdout
, and return codes.
We don’t have to install it with PIP, since it’s part of the Python standard library.
Therefore we can start using subprocesses in python just by importing the module.
First subprocess application
Pass arguments to the shell with subprocess
We’re running this command as a string and using the argument shell. That means we’re invoking a shell at the start of the execution of our subprocess, and the command argument is interpreted directly by the shell.
However, the use shell=True has many downsides, and the worst are the possible security leaks. You can read about them in the official documentation.
The best way to pass commands to the run function is to use a list where lst[0] is the command to call (ls in this case) and lst[n] are the arguments of that command.
If we do so, our code will look like this.
If we want to store the standard output of a subprocess in a variable, we can do it by setting the argument capture_output to true.
Usage Examples of subprocess
Program checker
One of the main usages of this library is the ability to make simple OS operations.
For instance, a simple script that checks if a program is installed. In Linux, we can do this with the which command.
Note: In UNIX when a command is successful its status code is 0. Otherwise, something went wrong during the execution
Since we’re not using the shell=True argument, we can take the user input securely. Also, we can check if the input is a valid program with a regex pattern.
In this case, we’re getting the programs from the user and using a regex expression that certifies the program string only includes letters and digits. We check the existence of each program with a for a loop.
Simple Grep in Python
Your friend Tom has a list of patterns in a text file and another large file in which he wants to get the number of matches for each pattern. He would spend hours running the grep command for every pattern.
Fortunately, you know how to solve this problem with Python, and you’ll help him to accomplish this task in few seconds.
Taking a look at this file, we define two variables which are the filenames we want to work with. Then we open the file that contains all the patterns and iterate over them. Next, we call a subprocess that runs a grep command with the “-c” flag (means count) and determine the output of the match with a conditional.
If you run this file (remember you can download the text files from the Github repo)
Set up a virtualenv with subprocess
One of the coolest things you can do with Python is process automation. This kind of script can save you hours of time per week.
For example, we’re going to create a setup script that creates a virtual environment for us and tries to find a requirements.txt file in the current directory to install all the dependencies.
In this case, we’re using multiple processes and parsing the data we need in our python script. We’re also using the pathlib library which allows us to figure it if the requirements.txt file exists.
If you run the python file you’ll get some useful messages of what’s happening with the OS.
Note that we get the output from the installation process because we’re not redirecting the standard output to a variable.
Run another Programming Language
We can run other programming languages with python and get the output from those files. This is possible because the subprocesses interact directly with the operative system.
For instance, let’s create a hello world program in C++ and Java. In order to execute the following file, you’ll need to install C++ and Java compilers.
helloworld.cpp
helloworld.java
I know this seems a lot of code compared to a simple Python one-liner, but this is just for testing purposes.
We’re going to create a Python script that runs all the C++ and Java files in a directory. To do this first we want to get a list of files depending on the file extension, and glob allows us to do it easily!
After that, we can start using subprocesses to execute each type of file.
Copy
One little trick is to use the string function strip to modify the output and only get what we need.
Note: Be carefully to run large Java or C++ files since we’re are loading their output in memory and that could produce a memory leak.
Open external programs
We’re able to run other programs just by calling their binaries location through a subprocess.
Let’s try it out by opening brave, my preferred web browser.
This will open a browser instance, or just another tab if you already have running the browser.
As with any other program that accept flags we can use them to produce the desired behavior.
Last updated