Skip to content

Using Subprocess Run in Python: Effortlessly Execute External Commands


The subprocess Module: Wrapping Programs With Python

by Ian Currie intermediate

If you’ve ever wanted to simplify your command-line scripting or use Python alongside command-line applications—or any applications for that matter—then the Python subprocess module can help. From running shell commands and command-line applications to launching GUI applications, the Python subprocess module can help.

By the end of this tutorial, you’ll be able to:

Understand how the Python subprocess module interacts with the operating system

  • Learn how to issue shell commands like ls or dir
  • Discover how to feed input into a process and use its output
  • Handle errors when using subprocess
  • Understand the use cases for subprocess by considering practical examples

Table of Contents

If you’re new to the subprocess module in Python, start with understanding the basics of processes and subprocesses. Learn about the interaction between processes and the operating system, the lifetime of processes, and how to identify active processes on your system.

Once you have a good understanding of processes, move on to the overview of the Python subprocess module. This will give you a high-level understanding of what the module does and how it can be used.

For basic usage of the subprocess module, explore the Timer Example, which demonstrates how to execute a command and measure the time it takes to complete. Then, learn how to use subprocess to run any application and capture its output. Finally, familiarize yourself with the CompletedProcess object and learn how it provides information about a completed subprocess.

Subprocess exceptions are important to handle when using the module. Understand the CalledProcessError for non-zero exit codes, TimeoutError for processes that take too long, and FileNotFoundError for programs that don’t exist. An example of exception handling will give you practical insights into how to handle errors in your code.

The shell and text-based programs are commonly used with subprocess. Learn about the use cases for the shell and subprocess and explore basic usage examples with UNIX-based shells and Windows shells. Pay attention to the security warning provided to ensure safe usage.

Communication with processes is a crucial aspect of using subprocess. Understand the standard I/O streams and how to interact with them. Explore examples such as the Magic Number Generator and the Reaction Game to get hands-on experience.

Pipes and the shell are concepts you’ll encounter when using subprocess. Understand the basics of pipes and how they work with subprocess. Gain practical knowledge by simulating pipes with the run() function.

Practical ideas include creating a new project using subprocess and changing extended attributes of files. These examples provide real-world scenarios where subprocess can be applied.

Finally, learn about other Python modules associated with subprocess and understand their use cases. This will expand your knowledge on leveraging subprocess in different contexts.

The Popen class is another key component of the subprocess module. Learn how to use Popen() to execute commands, connect two processes together with pipes, and interact dynamically with a process.

In conclusion, the Python subprocess module is a powerful tool for interacting with programs and processes. By understanding the concepts and examples provided in this tutorial, you’ll be able to use subprocess effectively in your Python projects. Start exploring the different topics and dive into the detailed explanations and executable sample codes to enhance your knowledge and skills.