我试图从一个目录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