In the official python docs, we can read that the subprocess should be used for accessing system commands. For example, the subprocess module enables us to spawn processes, connect to their input/output/error pipes, and receive return codes.
Python subprocess
Python subprocess module allows you to spawn new processes, connect to their input/output/error pipes, and obtain their return codes.
Python subprocess is a built-in module that enables you to start your Python program. In addition, the subprocess module provides a consistent interface for creating and working with additional processes.
The suggested approach to invoking subprocesses is to use the run() function for all use cases it can handle. The underlying Popen interface can be used directly for more advanced use cases.
The run() function was added in Python 3.5; if you need to preserve compatibility with older versions, see the Older high-level API section.
It offers a higher-level interface than some of the other available modules and is intended to replace functions such as os.system(), os.spawn*(), os.popen*(), popen2.*(), and commands.*().
To make it easier to compare the subprocess with those other modules, many examples here re-create the ones used for os and popen.
How to start a process in Python
To start a process in Python, use the subprocess.Popen() function.The program below starts the UNIX program “cat”, and the second parameter is an argument.
It is equivalent to ‘cat mod.py’. You can start the program with any parameter.
Now, inside your python project folder, create two files.
- app.py
- mod.py
Inside the mod.py, write the following code.
// mod.py student = { 'name': 'Krunal', 'enno': 21, 'college': 'vvp college' }
Now, write the following code inside the app.py file.
// app.py from subprocess import Popen, PIPE process = Popen(['cat', 'mod.py'], stdout=PIPE, stderr=PIPE) stdout, stderr = process.communicate() print(stdout)
See the below output.
The process.communicate() call reads the input and output from a process. The stdout is a process output. The stderr will be written only if the error occurs. If you want to wait for a program to finish, you can call Popen.wait() function.
Subprocess call()
Subprocess.call() is a built-in function that can be used to start a program. The call() function accepts a list of which the first argument must be the program name.
Syntax
subprocess.call(args, *, stdin=None, stdout=None, stderr=None, shell=False)
See the following code example.
It lists all the files and folders in the current directory.
Save process output (stdout)
We can get the program’s output and store it in the string directly using check_output. The method is defined as the following.
subprocess.check_output(args, *, stdin=None, stderr=None, shell=False, universal_newlines=False)
See the following example.
// app.py import subprocess s = subprocess.check_output(["echo", "AppDividend"]) print("s = " + s.decode('utf-8'))
See the below output.
Setting a shell argument to the true value causes the subprocess to spawn the intermediate shell process and tell it to run a command. The default is to run a command directly.
// app.py import subprocess # Command with shell expansion subprocess.call('echo $HOME', shell=True)
See the output.
Error Handling in Subprocess
The return value from the call() function is the program’s exit code. The caller is responsible for interpreting it to detect errors.
The check_call() function works like the call() function except that the exit code is checked, and if it indicates the error happened, then the CalledProcessError exception is raised.
See the following code.
// app.py import subprocess subprocess.check_call(['false'])
See the output.
That’s it for this tutorial.