Dhiwakar Ravikumar
Dhiwakar Ravikumar

Reputation: 2217

How to avoid script termination because of subprocess non-zero return code in python?

My Script always terminates abruptly because the command I'm executing using subprocess.check_call() or subprocess.check_output()

This is the Error.

  File "C:\Python34\lib\subprocess.py", line 620, in check_output
    raise CalledProcessError(retcode, process.args, output=output)

subprocess.CalledProcessError: Command '['C:\\Program Files\\info.exe','-student', 'Tony']' returned non-zero exit status 2

How do I avoid abnormal termination of the Script in such cases ? I've tried piping the Error/Exception to STDOUT but that didn't seem to help.

op = subprocess.check_output([installpath+'\\info.exe','-student', name],stderr=subprocess.STDOUT)

I did read that using subprocess.call(). I'm not sure if subprocess.call() is inferior to subprocess.check_output() & subprocess.check_call()

Upvotes: 0

Views: 1433

Answers (1)

Eli Korvigo
Eli Korvigo

Reputation: 10513

try:
    # start a subprocess
except CalledProcessError:
    # do something if it fails. 

Upvotes: 1

Related Questions