我試圖從一個目錄grep並限制搜索到前100個結果。下面的代碼保存得Python grep和管道通過Popen
[..]
grep: writing output: Broken pipe
grep: writing output: Broken pipe
grep: writing output: Broken pipe
grep: writing output: Broken pipe
[..]
代碼:
p_grep = Popen(['/bin/bash', '-c', 'grep -F "asdasdasd" data/*'], stdout = PIPE)
p_head = Popen(['head', '-100'], stdin = p_grep.stdout, stdout = PIPE)
output = p_head.communicate()[0]
如何解決呢?
試試這個: http://stackoverflow.com/questions/2595602/pythons-popen-cleanup – xkrz 2012-01-27 21:49:30
@xkrz,是不是他正在做的建議解決方案? – 2012-01-27 21:56:25
你需要執行grep還是僅僅是一個例子?否則,grep有一個--max-count選項,您可以使用而不是通過管道輸出整個輸出。 – GaretJax 2012-01-27 21:58:33