带有check_output的Python subprocess.call错误消息

时间:2012-10-24 13:10:21

标签: python subprocess

所以我有:

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_output更改为call,则可以正常工作:

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不起作用的原因是什么?

3 个答案:

答案 0 :(得分:6)

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

retcode = call(...)

然后打印返回码我猜你会看到它返回1.

答案 1 :(得分:3)

要在字符串中输出而不在非零退出状态上引发错误:

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

check_output()rc = p.poll()之后执行p.communicate(),并在bool(rc) == True时引发错误。

答案 2 :(得分: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()