Get exit code and stderr from subprocess call

后端 未结 5 2211
逝去的感伤
逝去的感伤 2020-12-14 05:07

I read up on the functions provided by subprocess - call, check_call, check_output, and understand how each works and differs in functionality from one another. I am curren

5条回答
  •  既然无缘
    2020-12-14 06:05

    Try this version:

    import subprocess
    try:
        output = subprocess.check_output(
            cmnd, stderr=subprocess.STDOUT, shell=True, timeout=3,
            universal_newlines=True)
    except subprocess.CalledProcessError as exc:
        print("Status : FAIL", exc.returncode, exc.output)
    else:
        print("Output: \n{}\n".format(output))
    

    This way you will print the output only if the call was successful. In case of a CalledProcessError you print the return code and the output.

提交回复
热议问题