0

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

enter image description here

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

1 Answer 1

2

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))
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for the link! I implemented the changes and will need to make some changes to my original post.

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.