我使用 find 和 wc 来获取使用管道的总 LOC。
find . -name "*.cpp" -print | xargs wc
44 109 896 ./main.cpp
...
288 1015 8319 ./src/util/util.cpp
733 2180 21494 total
我需要使用 python 自动获取 LOC,我将运行 find .. | xargs 命令多次,得到结果并处理得到总 LOC。
如何在 Python 中通过管道执行命令?我试过这个,但它什么也没返回。
import subprocess
p = subprocess.Popen(['find', '.', '-name', "*.cc", "-print", "|", "xargs", "wc"],
stdout=subprocess.PIPE,
stderr=subprocess.PIPE)
out, err = p.communicate()
print out
添加
在 konishchev 的提示下,我可以让它发挥作用。
p1 = Popen(['find', '.', '-name', "*.cc", "-print"], stdout=PIPE)
p2 = Popen(["xargs", "wc"], stdin=p1.stdout, stdout=PIPE)
p1.stdout.close() # Allow p1 to receive a SIGPIPE if p2 exits.
output = p2.communicate()[0]
print output