![]() |
VOOZH | about |
The subprocess.call() function in Python is used to run a command described by its arguments. Suppose you need to retrieve the output of the command executed by subprocess.call(). In that case, you'll need to use a different function from the subprocess module, such as subprocess.run(), subprocess.check_output(), or subprocess.Popen().
subprocess.call()The subprocess.call() function is used to execute a shell command. It waits for the command to complete and then returns the command's return code. This function is straightforward when you only need to know whether a command executed successfully or failed (based on the return code). However, it does not directly provide a method to capture the output of the command. For scenarios where capturing the output is necessary, other functions in the subprocess module, such as subprocess.Popen() or subprocess.run(), are more suitable.
subprocess.call()Here is the basic syntax of subprocess.call():
import subprocess
return_code = subprocess.call(args, *, stdin=None, stdout=None, stderr=None, shell=False)
args: This can be a string or a sequence of program arguments. The exact program to execute and its arguments.stdin, stdout, and stderr: These specify the executed program’s standard input, standard output, and standard error file handles, respectively.shell: If shell=True, the specified command will be executed through the shell.subprocess.call()While subprocess.call() is useful, it has its limitations, particularly:
subprocess.Popen().The subprocess.run() function allows you to capture the output by setting the capture_output parameter to True.
Output:
Return code: 0
Output: total 4
drwxr-xr-x 1 root root 4096 Jul 24 13:22 sample_data
Error:
The subprocess.check_output() function runs a command and returns its output as a byte string. You can decode this byte string to get a string.
Output:
Output: total 4
drwxr-xr-x 1 root root 4096 Jul 24 13:22 sample_data
The subprocess.Popen() function gives you more control over how you interact with the process. You can use it to capture both the standard output and standard error streams.
Output:
Output: total 4
drwxr-xr-x 1 root root 4096 Jul 24 13:22 sample_data
Here are practical examples of how to use subprocess.run(), subprocess.check_output(), and subprocess.Popen() in Python. These examples will demonstrate running a simple shell command and handling its outputs.
subprocess.run()The subprocess.run() function is a versatile tool introduced in Python 3.5 for managing subprocesses. It returns a CompletedProcess instance on completion.
Here’s an example of using subprocess.run() to execute a command and capture its output:
In this example:
capture_output=True ensures that the standard output and error are captured.text=True makes the output and error text-based instead of bytes, which is useful for processing in Python.Output:
Output: Hello, World!
Return Code: 0
subprocess.check_output()The subprocess.check_output() function runs a command and returns its output. If the command exits with a non-zero exit code, it raises a subprocess.CalledProcessError.
Here’s how to use subprocess.check_output():
This function:
Output:
Output: Hello, World!subprocess.Popen()The subprocess.Popen() class is used for more complex subprocess management, allowing fine control over I/O streams and the execution environment.
Here’s an example of using subprocess.Popen() to execute a command and capture its output:
In this example:
stdout=subprocess.PIPE and stderr=subprocess.PIPE indicate that the standard output and standard error of the command should be captured.process.communicate() waits for the command to complete and returns the output and errors.text=True ensures the outputs are returned as strings.Output:
Output: Hello, World!
Errors:
Return Code: 0
To retrieve the output of a command executed by subprocess.call(), you should switch to using subprocess.run(), subprocess.check_output(), or subprocess.Popen(). Each of these functions provides mechanisms to capture and handle the output of the executed command, which subprocess.call() does not offer.