While working in Python you may come across cases where you want a quick way to run programs from the command line. This could be your own python scripts or any other command line program. This is where the subprocess module can come into play.
The subprocess module, present in a standard installation of Python is used to run new applications or programs through Python code by creating new processes.
It gives us the ability to:
- spawn new processes
- connect to their input, output and error pipes
- obtain their return codes
A common use for subprocess would be to list files and folders in a directory. This makes use of the run method of subprocess.
The code will differ depending on what operating system you are on. For example, on a windows based computer this would be using the command dir
For Linux
import subprocess
subprocess.run('ls')
For Windows
import subprocess
subprocess.run("dir", shell=True)
In the code above we have:
- import subprocess module
- call the run module from it
- pass the list directory command based on your system (ls/dir). If you are on Windows you will have to additionally pass shell=True because dir is a shell command and you need to tell the system that you want to use it.
We automatically got output in the terminal even though we did not print it ourselves. That is how the run command works – it does not capture our command output by default.
Let’s see what happens if we try to capture our command by placing it in a variable
import subprocess
result = subprocess.run("dir", shell=True)
print(result)
The output is not what you would expect. We get the output still, and a message containing the arguments that were passed and the return code.
If we wanted to capture the output we would modify out code to this:
import subprocess
result = subprocess.run(["dir"], shell=True, capture_output=True, text=True)
print(result.stdout)
We have added:
-
capture_output=True
: to capture the output -
text=True
: to decode the output to a readable format since it is captured as bytes - We then
print result.stdout
: the result as standard output
The same can be achieved with the follow code:
import subprocess
result = subprocess.run(["ls", "-la"], stdout=subprocess.PIPE, text=True)
print(result.stdout)
The difference is stdout=subprocess.PIPE This code performs the same function as capture_output=True
In addition, we use the ls command with the argument of -la both passed as a list
There's more..
There's still more you can do with subprocess including outputting to a file, reading data from a file, and to trigger programs interactively to name a few things. Find out all of these and more at: Full Article at PythonHowToProgam
Top comments (0)