Python: get output from command line that exits with non-zero exit code

I am using Python 2.7.1 in a Windows Server 2008 R2 x64 window.

I am trying to get the result of a command line process that gives a non-zero exit status after the output of the information I need.

At first I used subprocess.check_output and caught a CalledProcessError that met with non-zero exit status, but while the return code was saved in error, not a single result found this.

Running this for cases that produce output but has exit status 0 works correctly, and I can get the output using subprocess.check_output.

My guess was that the output was written to STDOUT, but the exception deduced its “output” from STDERR. I tried to implement the check_output functionality, but I still don't get anything in the output when I consider that I should see the output of STDOUT and STDERR. My current code is below (where the "command" is the full text, including the parameters, of the command I am running:

 process = subprocess.Popen(command, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, universal_newlines=True) output = process.communicate() retcode = process.poll() if retcode: raise subprocess.CalledProcessError(retcode, image_check, output=output) return output 

This gives me the following in the output of the variable: [('', None)]

Is my subprocess.Popen code correct?

+6
python subprocess stdout stderr windows-server-2008-r2
source share
3 answers

You are working correctly. It turns out that the process you are calling probably outputs to CON. See the following example.

 import subprocess def check_output(command): process = subprocess.Popen(command, shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, universal_newlines=True) output = process.communicate() retcode = process.poll() if retcode: raise subprocess.CalledProcessError(retcode, command, output=output[0]) return output command = "echo this>CON" print "subprocess -> " + subprocess.check_output(command, shell=True) print "native -> " + str(check_output(command)) try: subprocess.check_output("python output.py", shell=True) except subprocess.CalledProcessError, e: print "subproces CalledProcessError.output = " + e.output try: check_output("python output.py") except subprocess.CalledProcessError, e: print "native CalledProcessError.output = " + e.output 

Exit

 subprocess -> native -> ('', None) stderr subproces CalledProcessError.output = stdout native CalledProcessError.output = stderr stdout 

Unfortunately, I do not know how to solve the problem. Note that subprocess.check_output results contain only output from stdout. Your replacement check_output will output both stderr and stdout.

After checking subprocess.check_output it actually throws a CalledProcessError with an output containing only stdout.

+8
source share

There is a problem here that might hit you - http://bugs.python.org/issue9905

+1
source share

You tried stderr=subprocess.STDOUT as indicated on the python doc page:

To also get the standard error as a result, use STDERR = subprocess.STDOUT:

Here is the test code:

 import subprocess try: subprocess.check_output('>&2 echo "errrrr"; exit 1', shell=True) except subprocess.CalledProcessError as e: print 'e.output: ', e.output try: subprocess.check_output('>&2 echo "errrrr"; exit 1', shell=True, stderr=subprocess.STDOUT) except subprocess.CalledProcessError as e: print 'e.output: ', e.output 

exit:

 errrrr e.output: e.output: errrrr 
+1
source share

All Articles