0

所以我有:

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不工作?

4

3 回答 3

6

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

retcode = call(...)

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

于 2012-10-24T13:12:58.383 回答
3

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

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

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

于 2012-10-24T13:21:13.113 回答
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()
于 2012-10-24T13:19:29.463 回答