The subprocess call suite
The subprocess module provides another function that makes process spawning a safer operation than using Popen()
. The subprocess call()
function waits for the called command/program to finish reading the output. It supports the same arguments as the Popen()
constructor, such as shell
, executable
, and cwd
, but this time, your script will wait for the program to complete and populate the return code without the need to communicate()
.
If you inspect the call()
function, you will see that it's actually a wrapper around the Popen()
class, but with a wait()
function that waits until the end of the command before returning the output:

import subprocess subprocess.call(["ifconfig", "docker0"], stdout=subprocess.PIPE, stderr=None, shell=False)
If you want more protection for your code, you can use the check_call()
function. It's the same as call()
, but adds another check to the return code. If it is equal to 0
(meaning that the command has successfully executed), then the...