Python subprocess.call错误消息和check_输出
因此,我:Python subprocess.call错误消息和check_输出,python,subprocess,Python,Subprocess,因此,我: result = subprocess.check_output(['wine', os.getcwd()+'/static/sigcheck.exe', '-a','-i','-q', self.tmpfile.path()]) 但是每当我运行这个程序时,我就会得到这个错误 CalledProcessError: Command '['wine', '/home
result = subprocess.check_output(['wine',
os.getcwd()+'/static/sigcheck.exe',
'-a','-i','-q',
self.tmpfile.path()])
但是每当我运行这个程序时,我就会得到这个错误
CalledProcessError: Command '['wine', '/home/static/sigcheck.exe', '-a', '-i', '-q', '/tmp/tmpxnsN5j']' returned non-zero exit status 1
但是如果我将检查输出
更改为调用
,它工作正常:
Z:\tmp\tmpvOybcm:
Verified: Unsigned
File date: 9:08 AM 10/24/2012
Publisher: Hardcore Computer
Description: Farthest Emitters Converter
Product: Farthest Emitters Converter
Version: 3.2.0
File version: 3.2.0
fixme:mscoree:StrongNameSignatureVerificationEx (L"Z:\\tmp\\tmpvOybcm", 1, 0x33ec13): stub
Strong Name: Unsigned
Original Name: n/a
Internal Name: Farthest Emitters Converter
Copyright: Hardcore Computer 2006
Comments: n/a
检查输出不起作用的原因是什么?非零返回码(通常)是程序错误退出的一种方式。因此,如果进程的返回代码非零,那么子进程.check_output
将引发异常。如果您使用:
retcode=call(…)
然后打印返回码,我想你会看到它返回1 替代方法
proc = subprocess.Popen(['wine',
os.getcwd()+'/static/sigcheck.exe',
'-a','-i','-q',
self.tmpfile.path()], stdin=subprocess.PIPE, stdout=subprocess.PIPE)
stdout = proc.stdout.read()
要在非零退出状态下获取字符串输出而不引发错误,请执行以下操作:
p = Popen(['wine',...], stdout=PIPE)
output = p.communicate()[0]
check\u output()
在p.communicate()
之后执行rc=p.poll()
,如果bool(rc)=True
有办法将输出捕获到变量中吗?您可以在中使用check\u output
尝试:
块和,调用的过程除外错误:
,但通常的方法只是使用管道为stdout、stderr创建一个subprocess.Popen
对象,然后使用它的communicate
方法。最好使用communicate()
而不是.stdout.read
以避免由于任何其他操作系统管道缓冲区填满并阻塞子进程而导致死锁。明白。谢谢你的警告