【问题标题】:python executing grep with subprocess not workingpython执行grep,子进程不起作用
【发布时间】:2015-07-01 03:04:15
【问题描述】:

尝试使用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
...

【问题讨论】:

    标签: python shell grep


    【解决方案1】:

    改成这样:

    except CalledProcessError as e:
        print(e.output)
    

    它会打印问题

    【讨论】:

    • 已经试过了。没有有用的错误信息。也许应该把它留在代码中来证明这一点。它,就这样:python % python find_docs_2.py command failed b''
    • 我所做的是从交互式提示运行它并将e 保存为全局值。然后我可以详细分析异常。
    猜你喜欢
    • 2011-12-18
    • 1970-01-01
    • 2014-10-14
    • 1970-01-01
    • 1970-01-01
    • 2013-10-15
    • 2015-03-11
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多