无法从(Python)subprocess.check_output()获取stdout / stderr [英] Can't get stdout/stderr from (Python) subprocess.check_output()

查看:872
本文介绍了无法从(Python)subprocess.check_output()获取stdout / stderr的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图从 git add 命令中获取消息,以便日后打印到日志文件。

I'm trying to get the message from a git add command, to print to a log file later on.

import subprocess
import os

filename = 'test.txt'

# Add changes
add_cmd = """git add "%s" """ % filename
os.system(add_cmd)
a = subprocess.check_output(add_cmd, shell=True, stderr=subprocess.STDOUT)

os.system()调用显示在屏幕上:

The os.system() call shows in screen:

fatal: Not a git repository (or any of the parent directories): .git

这是正确的,因为这个文件夹不是 git 回购。

which is correct, since this folder is not a git repo.

但是 subprocess.check_output()调用失败,并且:

But the subprocess.check_output() call fails with:

  File "test.py", line 11, in <module>
    a = subprocess.check_output(add_cmd, shell=True, stderr=subprocess.STDOUT)
  File "/usr/lib/python2.7/subprocess.py", line 573, in check_output
    raise CalledProcessError(retcode, cmd, output=output)
subprocess.CalledProcessError: Command 'git add "test.txt" ' returned non-zero exit status 128

为什么我无法通过 subprocess.check_output()?捕获错误消息?

Why am I not able to catch the error message with subprocess.check_output()?

推荐答案

$ b

From the documenation for subprocess.check_output():


$ subprocess.check_output()
: p>如果返回码不为零,则会引发 CalledProcessError CalledProcessError 对象将在 returncode 属性中具有返回码,并且输出中的任何输出属性。

If the return code was non-zero it raises a CalledProcessError. The CalledProcessError object will have the return code in the returncode attribute and any output in the output attribute.

git add 当出现错误情况时退出代码。赶上那个异常,你的输出就在那里:

git add returns a non-zero exit code when there is an error condition. Catch that exception, your output is there:

try:
    a = subprocess.check_output(add_cmd, shell=True, stderr=subprocess.STDOUT)
except subprocess.CalledProcessError as cpe:
    print cpe.output

演示:

Demo:

>>> import subprocess
>>> import os
>>> filename = 'test.txt'
>>> add_cmd = """git add "%s" """ % filename
>>> try:
...     a = subprocess.check_output(add_cmd, shell=True, stderr=subprocess.STDOUT)
... except subprocess.CalledProcessError as cpe:
...     print cpe.output
...
fatal: Not a git repository (or any of the parent directories): .git

>>> cpe.returncode
128

您可能不需要使用壳=真;相反,将你的参数作为列表传递并且它们将在没有中间壳的情况下执行。这有另外的好处,您不必担心正确转义 filename

You probably don't need to use shell=True; pass in your arguments as a list instead and they'll be executed without an intermediary shell. This has the added advantage you don't need to worry about properly escaping filename:

add_cmd = ['git', 'add', filename]
try:
    a = subprocess.check_output(add_cmd, stderr=subprocess.STDOUT)
except subprocess.CalledProcessError as cpe:
    print cpe.output

这篇关于无法从(Python)subprocess.check_output()获取stdout / stderr的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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