简体   繁体   中英

How do I get the console output of a subprocess regardless of the exit code

I am trying to call a subprocess and record the output of said process no matter whether it exits with an exit code of zero or non-zero.

Basically my subprocess will produce a bunch of debug output, part of which is an error code (like 404: Not Found) and I want to read that Error code from the output or the "Success" line if it exits with an exit code of zero.

I've written this code:

def run_subprocess(directory, command):
    os.chdir(directory)
    result = None
    out = None
    try:
        subprocess.call(command, shell=True)
        result = subprocess.check_output(command, shell=True, stderr=subprocess.STDOUT)
        print(colored("Subprocess succesful. Returning Result...", "green"))
        print("Result:\n", result)
        return result
    except subprocess.CalledProcessError as e:
        print(colored("Error Caught", "red"))
        out = e.output
        print("Output:\n", out)
        return out

The issue is, that sometimes the returned output will differ from the actual output of the subprocess that I can see on screen. For example, it'll say error 404, but the output of the function will include the error 500.

Now I'm completely lost on how or why the output is incorrect sometimes and sometimes not. In the end all I really need is the output of the subprocess regardless of the exit code so I can read lines from the output. Does anybody know what I could do to get this desired result while also avoiding random function outputs that don't align with the actual console output of the subprocess?

I resolved the issue by using subprocess.run() instead of subprocess.call() and capturing the output. This also ensured that the correct output was always returned.

Code:

def run_subprocess(directory, command):
    os.chdir(directory)
    result = None
    p1 = subprocess.run(command, shell=True, text=True, capture_output=True)
    result = p1.stdout
    print("Process Output:\n", result)
    return result

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM