Python:从以非零退出代码退出的命令行获取输出
我在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
你的代码工作正常。事实证明,您正在调用的进程可能正在输出到 CON。请参阅以下示例
输出
遗憾的是,我不知道如何解决该问题。请注意,
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
Output
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.您是否尝试过 python 文档页面中提到的
stderr=subprocess.STDOUT
:这是一个测试代码:
输出:
Have you tried
stderr=subprocess.STDOUT
as mentioned in the python doc page:Here is a test code:
output:
这里有一个问题可能会困扰你——
http://bugs.python.org/issue9905
There is an issue here that might be hitting you-
http://bugs.python.org/issue9905