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
ordir
- 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
- Processes and Subprocesses
- Processes and the Operating System
- Process Lifetime
- Active Processes on Your System
- Overview of the Python subprocess Module
- Basic Usage of the Python subprocess Module
- The Timer Example
- The Use of subprocess to Run Any App
- The CompletedProcess Object
- subprocess Exceptions
- CalledProcessError for Non-Zero Exit Code
- TimeoutExpired for Processes That Take Too Long
- FileNotFoundError for Programs That Don’t Exist
- An Example of Exception Handling
- Introduction to the Shell and Text-Based Programs With subprocess
- Use Cases for the Shell and subprocess
- Basic Usage of subprocess With UNIX-Based Shells
- Basic Usage of subprocess With Windows Shells
- A Security Warning
- Communication With Processes
- The Standard I/O Streams
- The Magic Number Generator Example
- The Decoding of Standard Streams
- Reaction Game Example
- Pipes and the Shell
- Introduction to Pipes
- The Pipes of subprocess
- Pipe Simulation With run()
- Practical Ideas
- Creating a New Project: An Example
- Changing Extended Attributes
- Python Modules Associated With subprocess
- The Popen Class
- Using Popen()
- Connecting Two Processes Together With Pipes
- Interacting Dynamically With a Process
- Conclusion
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.