Python Subprocess Module Tutorial – Complete Guide

Welcome to this exciting tutorial on the Python subprocess module – an invaluable tool you’ll likely use throughout your programming career. This guide provides a comprehensive insight into this module with engaging examples centered on simple game mechanics or real-life analogies so that you can grasp the concept easily.

Unveiling the Python Subprocess Module

The Python subprocess module allows you to spawn new processes, connect to their input/output/error pipes, and obtain their return codes. Essentially, this module helps you run command-line in the background without interrupting the user interface.

Think of it this way: if Python was a traffic policeman, the subprocess module would be his whistle and hand signals, controlling the flow of traffic (information) between the command line and other programs.

Learning about the subprocess module is an essential part of mastering Python, a language known for its versatility and simplicity. Understanding the subprocess module will allow you to write Python scripts interact with the system shell and execute non-Python programs, which can be quite useful, especially in system administration and automation tasks.

CTA Small Image
FREE COURSES AT ZENVA
LEARN GAME DEVELOPMENT, PYTHON AND MORE
ACCESS FOR FREE
AVAILABLE FOR A LIMITED TIME ONLY

Getting Started With Python subprocess Module

The Python subprocess module comes pre-installed with Python. To start using it, we have to import it first in your Python script.

import subprocess

Running Shell Commands with subprocess.run()

The most common use case for the subprocess module is to run shell commands. Use subprocess.run() to execute a command and wait for completion.

import subprocess
subprocess.run(["ls", "-l"])

The command is passed in as a list for proper handling of arguments. In our example, it will execute the “ls -l” command, listing the files in the current directory.

Capturing Output with subprocess.run()

We can capture the output of our shell command by passing an additional argument: capture_output=True:

import subprocess
result = subprocess.run(["echo", "Hello from the shell"], capture_output=True, text=True)
print(result.stdout)

In this example, we use the “echo” command to print “Hello from the shell” and capture its output.

Handling Errors with subprocess.run()

By default, subprocess.run() does not throw an exception if the command returns a non-zero exit code. Use check=True argument to change this behavior:

import subprocess
subprocess.run(["ls", "/non/existent/directory"], check=True)

In this example, the command will fail because we are trying to list a non-existent directory and because of the check=True argument, it will raise a CalledProcessError.

Running Command with Shell=True

If you have a command line as a string and want to execute it, use the shell=True argument:

import subprocess
subprocess.run("ls -l", shell=True)

In this example, we passed the command as a string instead of a list. The result would be the same as our initial example.

Stay tuned for the third part of the tutorial where we will discover advanced usage of the Python subprocess module!

Running Commands Interactively with subprocess.Popen()

The subprocess.Popen() function gives you more control when running shell commands. It returns an object that captures the ongoing state of the process:

import subprocess
p = subprocess.Popen(["sleep", "5"])

In this example, we launched a process that sleeps for 5 seconds. The script continues immediately; it does not wait for the sleep command to finish.

Waiting for Process Completion with Popen.wait()

Sometimes, we need to pause the Python script until the spawned process completes. We can achieve this using the Popen.wait() method:

import subprocess
p = subprocess.Popen(["sleep", "5"])
p.wait()

This script will actually pause for 5 seconds as it is waiting for the sleep command to complete.

Communicating with the Running Process

We can interact with the process while it is running using the Popen.communicate() method, which allows you to send input and receive output:

import subprocess
p = subprocess.Popen(["cat"], stdin=subprocess.PIPE, stdout=subprocess.PIPE, text=True)
out, err = p.communicate("Hello, World!")
print(out)

In this example, we passed “Hello, World!” to the `cat` command, which simply outputs what it receives.

Checking Process Return Code

After a process completes, you may want to check its return code, which is stored in the Popen.returncode attribute:

import subprocess
p = subprocess.Popen(["false"])
p.wait()
print(p.returncode)

The `false` command always returns a non-zero code. In our example, the output will be 1.

Executing shell piping commands

The subprocess module also provides a way to use shell piping commands:

import subprocess

grep_process = subprocess.Popen(["grep", "-v", "#"], stdin=subprocess.PIPE, text=True)
cut_process = subprocess.Popen(["cut", "-d", "-", "-f", "1"], stdin=grep_process.stdout, stdout=subprocess.PIPE, text=True)

grep_process.stdout.close()
out, err = cut_process.communicate()

print(out)

In the above example, ‘grep’ is used to filter out lines containing ‘#’, and ‘cut’ is cutting and presenting fields. This represents chaining of commands using pipe ( ‘|’) like in shell scripting.

By mastering the Python subprocess module, you will be able to play with shell commands in Python and create powerful tools and solutions for your coding project.

Where To Go Next With Python

Now that you have begun to master the Python subprocess module, you might be wondering how to continue down this exciting path of software development and programming mastery. At Zenva, we make the journey ahead an engaging and rewarding one, offering a host of courses designed to boost your career as you learn to code and create games.

An excellent place to keep expanding your Python skills is with our comprehensive Python Mini-Degree. The Python Mini-Degree covers a variety of important Python programming topics from coding basics and algorithms, to object-oriented programming and game development.

Our curriculum even delves into app development using popular Python libraries and frameworks like Pygame, Tkinter, and Kivy. The courses cater to all levels of learners – whether you’re just starting out or have been coding for years.

Apart from traditional tutorial content, our courses feature step-by-step projects, allowing you to apply new skills immediately and create your own games, algorithms, or apps.

For those looking for a wider selection of courses, you can browse our extensive range of Python courses.

Conclusion

Learning the Python subprocess module is an indispensable asset that will open up a myriad of possibilities in your coding journey. By understanding this tool, you’ll unlock the ability to create efficient, optimized Python programs – scripts that not only make your life easier but can also empower others.

With Zenva, you can fast track your development skills by exploring a host of insightful courses. We invite you to kick start, enhance, or solidify your Python knowledge with our Python Mini-Degree – an all-inclusive, comprehensive program meticulously designed to guide ambitious learners at every step of the way. So what are you waiting for? Embark on an exciting, rewarding journey of code discovery with Zenva today!

Did you come across any errors in this tutorial? Please let us know by completing this form and we’ll look into it!

FREE COURSES
Python Blog Image

FINAL DAYS: Unlock coding courses in Unity, Godot, Unreal, Python and more.