[파이썬] subprocess `subprocess.stderr`: 표준 오류 처리

In Python, the subprocess module provides a way to run external commands and manage their input, output, and errors. One important aspect of running commands is handling error messages or output from the standard error (stderr) stream. In this blog post, we will explore how to handle stderr using the subprocess module in Python.

Handling stderr with subprocess

When executing a command using subprocess, the standard output (stdout) and the standard error (stderr) streams are handled separately. By default, the stderr output is printed to the console. However, you can redirect and capture stderr for further processing or error handling.

Let’s look at an example:

import subprocess

command = ["ls", "-l", "nonexistentfile"]

try:
    result = subprocess.run(command, stderr=subprocess.PIPE)
    
    if result.returncode != 0:
        error_message = result.stderr.decode().strip()
        print(f"Command exited with error: {error_message}")
        
except FileNotFoundError:
    print("Command not found")

In the above example, we attempt to execute the ls -l nonexistentfile command using subprocess.run. Since the file nonexistentfile does not exist, an error message will be printed to stderr. We capture the stderr output by passing subprocess.PIPE as the stderr argument. This redirects stderr to a pipe, allowing us to access the error messages programmatically.

If the return code of the command is non-zero, we assume an error occurred. We decode the stderr output to a string and strip any leading or trailing whitespace. Finally, we print the error message.

Conclusion

Handling stderr output is an important part of running external commands in Python. The subprocess module provides a convenient way to capture and handle stderr messages. In this blog post, we explored how to redirect stderr to a pipe and process the error messages programmatically. By effectively handling stderr, you can improve the robustness of your Python scripts and handle errors gracefully.