Python:从以非零退出代码退出的命令行获取输出

发布于 2024-10-17 11:48:39 字数 816 浏览 1 评论 0原文

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

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

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

针对给出输出但退出状态为 0 的情况运行此命令可以正常工作,我可以使用 subprocess.check_output 获取输出。

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

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)]

Is我的 subprocess.Popen 代码正确吗?

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.

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.

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.

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 

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

Is my subprocess.Popen code correct?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

沧笙踏歌 2024-10-24 11:48:39

你的代码工作正常。事实证明,您正在调用的进程可能正在输出到 CON。请参阅以下示例

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。

在检查 subprocess.check_output 后​​,它确实生成了一个 CalledProcessError ,其输出仅包含 stdout。

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

Output

subprocess -> 
native -> ('', None)
stderr subproces CalledProcessError.output = stdout
native CalledProcessError.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.

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

爱的那么颓废 2024-10-24 11:48:39

您是否尝试过 python 文档页面中提到的 stderr=subprocess.STDOUT

要捕获结果中的标准错误,请使用
stderr=子进程.STDOUT:

这是一个测试代码:

import subprocess

try:
    subprocess.check_output('>&2 echo "errrrr"; exit 1', shell=True)
except subprocess.CalledProcessError as e:
    print 'e.output: ', e.output


try:
    subprocess.check_output('>&2 echo "errrrr"; exit 1', shell=True, stderr=subprocess.STDOUT)
except subprocess.CalledProcessError as e:
    print 'e.output: ', e.output

输出:

errrrr
e.output:  
e.output:  errrrr

Have you tried stderr=subprocess.STDOUT as mentioned in the python doc page:

To also capture standard error in the result, use
stderr=subprocess.STDOUT:

Here is a test code:

import subprocess

try:
    subprocess.check_output('>&2 echo "errrrr"; exit 1', shell=True)
except subprocess.CalledProcessError as e:
    print 'e.output: ', e.output


try:
    subprocess.check_output('>&2 echo "errrrr"; exit 1', shell=True, stderr=subprocess.STDOUT)
except subprocess.CalledProcessError as e:
    print 'e.output: ', e.output

output:

errrrr
e.output:  
e.output:  errrrr
当梦初醒 2024-10-24 11:48:39

这里有一个问题可能会困扰你——
http://bugs.python.org/issue9905

There is an issue here that might be hitting you-
http://bugs.python.org/issue9905

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文