尝试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
...
答案 0 :(得分:0)
将其更改为:
except CalledProcessError as e:
print(e.output)
它将打印问题