擅长:python、mysql、java
<p>使用<a href="http://pypi.python.org/pypi/sh" rel="nofollow">sh module</a>,管道成为函数组合:</p>
<pre><code>import sh
output = sh.thirdcmd(sh.mysecondcmd(sh.mycmd("arg1")))
</code></pre>
<hr/>
<p>如果您想用没有<code>shell = True</code>的子进程来完成它,有一个<a href="http://docs.python.org/library/subprocess.html#replacing-shell-pipeline" rel="nofollow">example in the docs</a>,它展示了如何使用<code>subprocess.Popen</code>编写shell管道。请注意,您应该关闭<code>proc.stdout</code>s,以便能够正确接收<code>SIGPIPE</code>s:</p>
^{pr2}$
<p>这看起来比使用<code>shell = True</code>要多得多。您可能希望避免<code>shell = True</code>的原因是<a href="http://docs.python.org/2/library/subprocess.html#frequently-used-arguments" rel="nofollow">it can be a security risk</a>(向下翻页到“警告”框),特别是当您运行的命令是由(潜在的恶意)用户提供的。在</p>