Python:从以非零退出代码退出的命令行获取输出 [英] Python: get output from a command line which exits with nonzero exit code

查看:82
本文介绍了Python:从以非零退出代码退出的命令行获取输出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 Windows Server 2008 R2 x64 机器上使用 Python 2.7.1.

I am Using Python 2.7.1 on a Windows Server 2008 R2 x64 box.

我正在尝试获取命令行进程的输出,该进程在输出我需要的信息后给出非零退出状态.

I'm trying to get the output of a command line process which gives a nonzero exit status after outputting the information I need.

我最初使用 subprocess.check_output,并捕获出现非零退出状态的 CalledProcessError,但是当返回码存储在错误中时,没有输出显示这一点.

I was initially using subprocess.check_output, and catching the CalledProcessError which occurs with nonzero exit status, but while the returncode was stored in the error, no output revealed this.

针对提供输出但退出状态为 0 的情况运行此程序可以正常工作,我可以使用 subprocess.check_output 获取输出.

Running this against cases which give output but have an exit status of 0 works properly and I can get the output using subprocess.check_output.

我的假设是输出正在写入 STDOUT,但异常从 STDERR 中提取其输出".我试图重新实现 check_output 的功能,但是当我相信我应该看到输出到 STDOUT 和 STDERR 时,我仍然没有得到任何输出.我当前的代码如下(其中命令"是我正在运行的命令的全文,包括参数:

My assumption was that the output was being written to STDOUT but the exception pulls its 'output' from STDERR. I've tried to re implement the functionality of check_output, but I still get nothing on the output when I believe I should be seeing output to STDOUT and STDERR. My current code is below (where 'command' is the full text, including parameters, of command I am running:

process = subprocess.Popen(command, stdout=subprocess.PIPE, 
stderr=subprocess.STDOUT, universal_newlines=True)
output = process.communicate()
retcode = process.poll()
if retcode:
    raise subprocess.CalledProcessError(retcode, image_check, output=output)
    return output 

这在变量输出中给了我以下内容:[('', None)]

This gives me the following in the variable output: [('', None)]

我的 subprocess.Popen 代码是否正确?

Is my subprocess.Popen code correct?

推荐答案

您的代码运行良好.事实证明,您正在调用的进程可能正在输出到 CON.看下面的例子

You code works fine. Turns out that the process that you are calling is probably outputing to CON. See the following example

import subprocess

def check_output(command):
    process = subprocess.Popen(command, shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, universal_newlines=True)
    output = process.communicate()
    retcode = process.poll()
    if retcode:
            raise subprocess.CalledProcessError(retcode, command, output=output[0])
    return output 

command = "echo this>CON"

print "subprocess -> " + subprocess.check_output(command, shell=True)
print "native -> " + str(check_output(command))

try:
    subprocess.check_output("python output.py", shell=True)
except subprocess.CalledProcessError, e:
    print "subproces CalledProcessError.output = " + e.output

try:
    check_output("python output.py")
except subprocess.CalledProcessError, e:
    print "native CalledProcessError.output = " + e.output

输出

subprocess -> 
native -> ('', None)
stderr subproces CalledProcessError.output = stdout
native CalledProcessError.output = stderr stdout

遗憾的是,我不知道如何解决这个问题.请注意,subprocess.check_output 结果仅包含来自 stdout 的输出.您的 check_output 替换将输出 stderr 和 stdout.

Sadly, I do not know how to resolve the issue. Notice that subprocess.check_output results contains only the output from stdout. Your check_output replacement would output both stderr and stdout.

在检查 subprocess.check_output 之后,它确实生成了一个 CalledProcessError,输出只包含 stdout.

After inspecting subprocess.check_output, it does indeed generate a CalledProcessError with the output containing only stdout.

这篇关于Python:从以非零退出代码退出的命令行获取输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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