当可执行文件失败时从subprocess.Popen()捕获错误

问题描述 投票:0回答:1

任何人都可以推荐对以下脚本进行一些改进,以便在Application.exe文件失败时,脚本结束而不是无限期地挂起?

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()

当应用程序文件失败时,没有抛出异常,是否可以让子进程在这样的事件中抛出一个?


编辑:实现了p.returncode语句。

要修改我的原始问题,当Application.exe程序失败时,它将显示以下窗口

enter image description here

只有在我关闭此窗口后,程序才会继续(例如:p.returncode将返回值255)。

有没有办法让这个窗口不显示或自动关闭或尽管显示此窗口仍然继续程序?

问候

python error-handling subprocess
1个回答
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))
© www.soinside.com 2019 - 2024. All rights reserved.