I use subprocess.check_output a number of times in a script of mine, but I just ran into an issue with an external application. OpenVPN in this case.
When openvpn is called with the --help parameter, which I want to parse in my script, it returns 1 as its exit code. check_ouput chokes on the non-zero exit code and fails with the following message:
subprocess.CalledProcessError: Command '['openvpn', '--help']' returned non-zero exit status 1
Q: I don't understand why openvpn does this, but how can I have check_output give me the output, even with a non-zero return code?
edit: I used the exact same code with --show-digests or other parameters and all seemed to work just fine.
output = check_output(["openvpn", "--show-digests"])
subprocess.run()which is a more versatile overall design. It takes some getting used to, but it will produce an object which contains the output, the exit code, and a smattering of additional state information for the process you ran. In brief, you wantresult = subprocess.run(['openvpn', '--help'], stdout=subprocess.PIPE, universal_newlines=True).stdout(nocheck=Truebecause you expect it to fail).