2

How can I get the exit code of subprocess.check_output?
I'm assuming the returncode should be 0 if a file is found matching my pattern and non-zero if no files are found matching the pattern?
If there is an exception, I'm recieving a non-zero returncode, as expected.

try:
        output = subprocess.check_output(["staf", "server.com", "PROCESS", "START", "SHELL", "COMMAND", "ls *heapdump*", "WAIT", "RETURNSTDOUT", "STDERRTOSTDOUT"])
        print result
except CalledProcessError as e:
        print(e.returncode)
        sys.exit(e.returncode)

1 Answer 1

4

Just as the documentation says:

If the return code was non-zero it raises a CalledProcessError. The CalledProcessError object will have the return code in the returncode attribute and any output in the output attribute.

So, if you don't end up in your except block, you can assume 0 was returned. You're already doing the right thing to handle non-zero return codes.

Sign up to request clarification or add additional context in comments.

2 Comments

is there an alternative to subprocess.check_output, which would return non-zero if file is not found and 0 if file is found? Similiar to the way bash would operate.
Do you care about the output of the subprocess? If not, you can use subprocess.call. If you do, you need to use p = subprocess.Popen(...,stdout=subprocess.PIPE), and then check p.returncode after the process is done.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.