Python subprocess.Popen.wait()返回0,即使发生错误 [英] Python subprocess.Popen.wait() returns 0 even though an error occured

查看:432
本文介绍了Python subprocess.Popen.wait()返回0,即使发生错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我通过Python的子进程模块运行命令行实用程序。我使用命令行参数和stdout = subprocess.PIPE创建一个subprocess.Popen()对象,然后我使用subprocess.wait()等待任务完成,并返回应该指示任务是否成功完成的返回代码。

I am running a command line utility via Python's subprocess module. I create a subprocess.Popen() object with the command line arguments and stdout=subprocess.PIPE and then I use subprocess.wait() to wait until the task is complete and to return the return code which should indicate whether the task completed successfully.

translate = subprocess.Popen(['gdal_translate', '-of', 'HFA', 'inDataset', 'outDataset'], stdout=subprocess.PIPE)
if translate.wait() == 0: print "Success"
else: print "Fail - %s" % translate.stdout.read()

如果我在Windows命令提示符下运行我的脚本,我可以看到gdal_translate提供的消息,是导致进程失败的错误,但我的脚本仍然打印成功。

If I run my script in the windows command prompt I can see the messages provided by gdal_translate and they say there were errors that cause a process to fail, but my script still prints "Success".

如果返回代码是总是0?

How can I check for errors reported by the command line utility if the return code is always 0?

推荐答案

有可能输出错误,并且返回码为零。在您的代码中,您只捕获标准输出,而不是标准错误。在下面的run函数中,它将运行一个命令,等待执行结束,然后读取returncode标准错误和标准输出。如果有任何标准错误或如果returncode不为零,那么它会将其视为失败。您可以在代码中看到四个示例调用。第一个是正常的成功调用,第二个返回代码为0,但有错误和标准输出的输出。第三个具有非零返回码和错误输出,而最后一个示例具有非零返回码,根本没有输出。

It is possible that you get errors being outputted and still have a return code of zero. In your code you only capture standard out and not standard error. In the run function below it will run a command, wait for execution to end, then read the returncode standard error and standard output. If there is anything on standard error or if returncode is not zero then it will treat that as a failure. You can see the four example calls in the code. The first one is a normal successful call, the second has return code 0 but has output on error and standard output. The third has a non-zero return code and error output, while the last example has a none-zero return code with no output at all.

code

from subprocess import Popen, PIPE

def run(cmd):
    print '-'*40
    print 'running:', cmd
    p = Popen(cmd, stderr=PIPE, stdout=PIPE, shell=True)
    output, errors = p.communicate()
    print [p.returncode, errors, output]
    if p.returncode or errors:
        print 'something went wrong...'

run("echo all is well")
run("echo out;echo error 1>&2")
run("this-will-fail")
run("exit 1")

输出

----------------------------------------
running: echo all is well
[0, '', 'all is well\n']
----------------------------------------
running: echo out;echo error 1>&2
[0, 'error\n', 'out\n']
something went wrong...
----------------------------------------
running: this-will-fail
[127, '/bin/sh: this-will-fail: not found\n', '']
something went wrong...
----------------------------------------
running: exit 1
[1, '', '']
something went wrong...

这篇关于Python subprocess.Popen.wait()返回0,即使发生错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆