Using the subprocess module
Python is really useful in situations where we need to start and communicate with other programs on the OS.
The subprocess
module allows us to start a new process and communicate with it, bringing to Python all the available tools installed on your OS through an easy-to-use API. The subprocess
module can be seen by calling any other program from your shell.
This module has gone through some work to modernize and simplify its API, and you might see code using subprocess
in ways different from those shown in this topic.
The subprocess
module has two main APIs: the subprocess.run
call, which manages everything from you passing the right arguments, and subprocess.Popen
, a lower-level API that is available for more advanced use cases. You are going to cover only the high-level API, subprocess.run
, but if you need to write an application that requires something more complex, as we have previously seen with the Standard Library, go through the documentation...