Open In App

Retrieving the output of subprocess.call() in Python

Last Updated : 01 Aug, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

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.


Next Article
Article Tags :
Practice Tags :

Similar Reads