Retrieving the output of subprocess.call() in Python
Last Updated :
01 Aug, 2024
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().
Introduction to 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.
Syntax of 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.
Limitations of subprocess.call()
While subprocess.call()
is useful, it has its limitations, particularly:
- No direct output capture: It does not capture the stdout or stderr of the command. It only returns the exit status of the command.
- Less control over execution: It provides less control over process execution compared to
subprocess.Popen()
.
Using subprocess.run()
The subprocess.run() function allows you to capture the output by setting the capture_output parameter to True.
Python
import subprocess
result = subprocess.run(['ls', '-l'], capture_output=True, text=True)
print("Return code:", result.returncode)
print("Output:", result.stdout)
print("Error:", result.stderr)
Output:
Return code: 0
Output: total 4
drwxr-xr-x 1 root root 4096 Jul 24 13:22 sample_data
Error:
Using subprocess.check_output()
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.
Python
import subprocess
output = subprocess.check_output(['ls', '-l'], text=True)
print("Output:", output)
Output:
Output: total 4
drwxr-xr-x 1 root root 4096 Jul 24 13:22 sample_data
Using subprocess.Popen()
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.
Python
import subprocess
process = subprocess.Popen(['ls', '-l'], stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True)
stdout, stderr = process.communicate()
print("Return code:", process.returncode)
print("Output:", stdout)
print("Error:", stderr)
Output:
Output: total 4
drwxr-xr-x 1 root root 4096 Jul 24 13:22 sample_data
Example Usage
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.
1. Example with 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.
Python
import subprocess
# Run the 'echo' command and capture its output
result = subprocess.run(["echo", "Hello, World!"], capture_output=True, text=True)
# Accessing the output
print("Output:", result.stdout)
print("Return Code:", result.returncode)
Output:
Output: Hello, World!
Return Code: 0
2. Example with 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:
- Captures and returns the standard output of the command.
- Raises an exception if the command fails (non-zero exit status).
Python
import subprocess
try:
# Run the 'echo' command and capture its output
output = subprocess.check_output(["echo", "Hello, World!"], text=True)
print("Output:", output)
except subprocess.CalledProcessError as e:
print("Failed to run command:", e)
Output:
Output: Hello, World!
3. Example with 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.
Python
import subprocess
# Command to execute
command = ["echo", "Hello, World!"]
# Start the subprocess
process = subprocess.Popen(command, stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True)
# Wait for the command to complete and get the output
stdout, stderr = process.communicate()
print("Output:", stdout)
print("Errors:", stderr)
print("Return Code:", process.returncode)
Output:
Output: Hello, World!
Errors:
Return Code: 0
Conclusion
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.
Similar Reads
Python | Logging Test Output to a File Problem - Writing the results of running unit tests to a file instead of printed to standard output. A very common technique for running unit tests is to include a small code fragment (as shown in the code given below) at the bottom of your testing file. Code #1 : Python3 1== import unittest class M
2 min read
Python subprocess module The subprocess module present in Python(both 2.x and 3.x) is used to run new applications or programs through Python code by creating new processes. It also helps to obtain the input/output/error pipes as well as the exit codes of various commands. In this tutorial, weâll delve into how to effective
9 min read
Handling Access Denied Error Occurs While Using Subprocess.Run in Python In Python, the subprocess module is used to run new applications or programs through Python code by creating new processes. However, encountering an "Access Denied" error while using subprocess.run() can be problematic. This error arises due to insufficient permissions for the user or the Python scr
5 min read
Returning a function from a function - Python In Python, functions are first-class objects, allowing them to be assigned to variables, passed as arguments and returned from other functions. This enables higher-order functions, closures and dynamic behavior.Example:Pythondef fun1(name): def fun2(): return f"Hello, {name}!" return fun2 # Get the
5 min read
How can we display an image in a child process in Python Python allows multiprocessing to efficiently utilize multiple CPU cores for concurrent tasks. Displaying an image in a child process involves spawning a separate process using libraries like Tkinter or Matplotlib. This approach makes sure that image loading and display operations can run concurrentl
2 min read
Python PRAW - Getting the subreddit on which a comment is posted in Reddit In Reddit, we can post a comment to any submission, we can also comment on a comment to create a thread of comments. Here we will see how to fetch the subreddit in which the comment has been posted using PRAW. We will be using the subreddit attribute of the Comment class to fetch subreddit class of
2 min read
Multiprocessing in Python | Set 1 (Introduction) This article is a brief yet concise introduction to multiprocessing in Python programming language. What is multiprocessing? Multiprocessing refers to the ability of a system to support more than one processor at the same time. Applications in a multiprocessing system are broken to smaller routines
7 min read
How to print to stderr and stdout in Python? In Python, whenever we use print() the text is written to Pythonâs sys.stdout, whenever input() is used, it comes from sys.stdin, and whenever exceptions occur it is written to sys.stderr. We can redirect the output of our code to a file other than stdout. But you may be wondering why one should do
3 min read
How to call a function in Python Python is an object-oriented language and it uses functions to reduce the repetition of the code. In this article, we will get to know what are parts, How to Create processes, and how to call them.In Python, there is a reserved keyword "def" which we use to define a function in Python, and after "de
5 min read
Matplotlib.pyplot.subplot_tool() in Python Matplotlib is a library in Python and it is numerical - mathematical extension for NumPy library. Pyplot is a state-based interface to a Matplotlib module which provides a MATLAB-like interface. Sample Code Python3 1== # sample code import matplotlib.pyplot as plt plt.plot([1, 2, 3, 4], [16, 4, 1, 8
1 min read