python执行grep与子进程不工作

时间:2015-07-01 03:04:16

标签: python shell grep

尝试subprocess做一个递归grep。这是代码:

import subprocess
from subprocess import CalledProcessError, check_output

try:
    out = check_output(
        ["grep",
         "-R",
         '--include="*.org"',
         "abc",
         "/home/fenton/projects/docs-DIR/documentation/"],
        stderr=subprocess.STDOUT)
    print(out)
except CalledProcessError as e:
    print("command failed")

当我运行它时,它会失败。

python % python find_docs_2.py
command failed

从命令行执行此操作成功:

python % grep -R --include="*.org" abc /home/fenton/projects/docs-DIR/documentation/
/home/fenton/projects/docs-DIR/documentation/zsh.org:    % ./test.sh -f abc
/home/fenton/projects/docs-DIR/documentation/zsh.org:    abc
...

1 个答案:

答案 0 :(得分:0)

将其更改为:

except CalledProcessError as e:
    print(e.output)

它将打印问题