2012-10-24 52 views
0

错误信息所以我必须:的Python subprocess.call与check_output

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 

但是,如果我改变check_outputcall它工作正常:

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不起作用?

回答

6

非零返回码(通常)是一种通过程序指示错误退出的方式。因此,如果进程的返回码非零,则subprocess.check_output将引发异常。如果你使用:

retcode = call(...)

,然后打印返回代码我想你会看到,它返回1

+0

有没有办法将输出捕获到变量中? –

+1

你可以在'try:'块中使用'check_output'和'除了CalledProcessError:',但通常的方法是使用管道为stdout,stderr创建一个'subprocess.Popen'对象,然后使用它的'communic''方法。 – wim

+0

谢谢!欣赏这一点。 –

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

最好使用'communicate()'而不是'.stdout.read'来避免由于任何其他OS管道缓冲区填满和阻塞子进程而导致的死锁。 – wim

+0

罗杰。谢谢你的提醒 – SWAPYAutomation

3

要在字符串中获取输出,而不引起非零退出状态的错误:

p = Popen(['wine',...], stdout=PIPE) 
output = p.communicate()[0] 

check_output()p.communicate()之后执行rc = p.poll(),并且如果bool(rc) == True产生错误。