当前位置:   article > 正文

python stdout stderr,无法从(Python)subprocess.check_output()获取stdout / stderr

except subprocess.calledprocesserror, cpe: ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

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)

The os.system() call shows in screen:

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

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

But the subprocess.check_output() call fails with:

File "test.py", line 11, in

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

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

解决方案

From the documenation for subprocess.check_output():

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

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

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/从前慢现在也慢/article/detail/218745
推荐阅读
相关标签
  

闽ICP备14008679号