Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/337.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python subprocess.call错误消息和check_输出_Python_Subprocess - Fatal编程技术网

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
以避免由于任何其他操作系统管道缓冲区填满并阻塞子进程而导致死锁。明白。谢谢你的警告