擅长:python、mysql、java
<p>在经历了<a href="https://blog.nelhage.com/2010/02/a-very-subtle-bug/" rel="nofollow noreferrer">blog</a>之后,我得到了以下解决方案:)</p>
<pre><code>import subprocess
import signal
output = subprocess.check_output('grep string tsvfile', shell=True, preexec_fn=lambda: signal.signal(signal.SIGPIPE, signal.SIG_DFL))
print output
</code></pre>
<p><strong>提示:</strong></p>
<ul>
<li>如果找不到字符串,<code>grep</code>以退出代码1结尾,check\u输出将引发异常。在</li>
<li>check_输出从python2.7开始就可用了。另一种外观<a href="https://stackoverflow.com/a/4814985/3912144">here</a>。在</li>
</ul>