Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check a command's return code when subprocess raises a CalledProcessError exception

Tags:

I want to capture the stdout stream of a shell command in a python (3) script, and being able, at the same time, to check the return code of the shell command if it returns an error (that is, if its return code is not 0).

subprocess.check_output seems to be the appropriate method to do this. From subprocess's man page:

check_output(*popenargs, **kwargs)     Run command with arguments and return its output as a byte string.      If the exit code was non-zero it raises a CalledProcessError.  The     CalledProcessError object will have the return code in the returncode     attribute and output in the output attribute. 

Still, I don't succeed to obtain the return code from the shell command when it fails. My code looks like this:

import subprocess failing_command=['ls', 'non_existent_dir']  try:     subprocess.check_output(failing_command) except:     ret = subprocess.CalledProcessError.returncode  # <- this seems to be wrong     if ret in (1, 2):         print("the command failed")     elif ret in (3, 4, 5):         print("the command failed very much") 

This code raises an exception in the handling of the exception itself:

Traceback (most recent call last):   File "<stdin>", line 4, in <module> AttributeError: type object 'CalledProcessError' has no attribute 'returncode' 

I admit I don't know where I am wrong.

like image 301
michaelmeyer Avatar asked Mar 09 '13 22:03

michaelmeyer


People also ask

Does subprocess call raise exception?

The Python subprocess call() function returns the executed code of the program. If there is no program output, the function will return the code that it executed successfully. It may also raise a CalledProcessError exception.

What does subprocess Check_call return?

subprocess. check_call() gets the final return value from the script, and 0 generally means "the script completed successfully".


2 Answers

To get both the process output and the returned code:

from subprocess import Popen, PIPE  p = Popen(["ls", "non existent"], stdout=PIPE) output = p.communicate()[0] print(p.returncode) 

subprocess.CalledProcessError is a class. To access returncode use the exception instance:

from subprocess import CalledProcessError, check_output  try:     output = check_output(["ls", "non existent"])     returncode = 0 except CalledProcessError as e:     output = e.output     returncode = e.returncode  print(returncode) 
like image 176
jfs Avatar answered Oct 30 '22 06:10

jfs


Most likely my answer is no longer relevant, but I think it may be solved with this code:

import subprocess failing_command='ls non_existent_dir'  try:     subprocess.check_output(failing_command, shell=True, stderr=subprocess.STDOUT) except subprocess.CalledProcessError as e:     ret =   e.returncode      if ret in (1, 2):         print("the command failed")     elif ret in (3, 4, 5):         print("the command failed very much") 
like image 34
Nodari Lipartiya Avatar answered Oct 30 '22 05:10

Nodari Lipartiya