Could anyone recommend some improvements to the following script such that if the Application.exe file fails, the script ends rather than just becoming hanging indefinitely?
application = r'.\Application.exe'
input_params = '-i Input_1 -j Input_2
process = (application,input_params)
p = subprocess.Popen(" ".join(process),shell= True, stderr=subprocess.STDOUT, stdout = subprocess.PIPE)
p.communicate()
When the application file failed there was no exception thrown, is it possible to have subprocess throw one in such an event?
Edit: Implemented the p.returncode statement.
To revise my original question, when the Application.exe program fails it displays the following window
It is only after I close this window does the program proceed (eg: p.returncode will return the value 255).
Is there a way to either have this window not be displayed or automatically closed or have the program proceed despite having this window displayed?
Regards
答案 0 :(得分:1)
How to catch exception output from Python subprocess.check_output()?
try:
subprocess.check_output(...)
except subprocess.CalledProcessError as e:
print e.output
from subprocess import Popen, PIPE
p = Popen(['bitcoin', 'sendtoaddress', ..], stdout=PIPE, stderr=PIPE)
output, error = p.communicate()
if p.returncode != 0:
print("bitcoin failed %d %s %s" % (p.returncode, output, error))