擅长:python、mysql、java
<p>从我很久以前问过的“<a href="https://stackoverflow.com/questions/1410976/equivalent-of-backticks-in-python">Equivalent of Bash Backticks in Python</a>”中,您可能想要使用的是<code>popen</code>:</p>
<pre><code>os.popen('cat /etc/services').read()
</code></pre>
<p>从<a href="https://docs.python.org/3.6/library/os.html#os.popen" rel="noreferrer">docs for Python 3.6</a></p>
<blockquote>
<p>This is implemented using subprocess.Popen; see that class’s
documentation for more powerful ways to manage and communicate with
subprocesses.</p>
</blockquote>
<hr/>
<p>以下是<code>subprocess</code>的对应代码:</p>
<pre><code>import subprocess
proc = subprocess.Popen(["cat", "/etc/services"], stdout=subprocess.PIPE, shell=True)
(out, err) = proc.communicate()
print "program output:", out
</code></pre>