Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>The answer to this question depends on the version of Python you're using. The simplest approach is to use the <a href="https://docs.python.org/3/library/subprocess.html#subprocess.check_output" rel="noreferrer"><code>subprocess.check_output</code></a> function:</p> <pre><code>&gt;&gt;&gt; subprocess.check_output(['ls', '-l']) b'total 0\n-rw-r--r-- 1 memyself staff 0 Mar 14 11:04 files\n' </code></pre> <p><code>check_output</code> runs a single program that takes only arguments as input.<sup>1</sup> It returns the result exactly as printed to <code>stdout</code>. If you need to write input to <code>stdin</code>, skip ahead to the <code>run</code> or <code>Popen</code> sections. If you want to execute complex shell commands, see the note on <code>shell=True</code> at the end of this answer.</p> <p>The <code>check_output</code> function works on almost all versions of Python still in wide use (2.7+).<sup>2</sup> But for more recent versions, it is no longer the recommended approach.</p> <h3>Modern versions of Python (3.5 or higher): <code>run</code></h3> <p>If you're using <strong>Python 3.5</strong> or higher, and <strong>do not need backwards compatibility</strong>, the <a href="https://docs.python.org/3/library/subprocess.html#using-the-subprocess-module" rel="noreferrer">new <code>run</code> function</a> is recommended. It provides a very general, high-level API for the <code>subprocess</code> module. To capture the output of a program, pass the <code>subprocess.PIPE</code> flag to the <code>stdout</code> keyword argument. Then access the <code>stdout</code> attribute of the returned <a href="https://docs.python.org/3/library/subprocess.html#subprocess.CompletedProcess" rel="noreferrer"><code>CompletedProcess</code></a> object:</p> <pre><code>&gt;&gt;&gt; import subprocess &gt;&gt;&gt; result = subprocess.run(['ls', '-l'], stdout=subprocess.PIPE) &gt;&gt;&gt; result.stdout b'total 0\n-rw-r--r-- 1 memyself staff 0 Mar 14 11:04 files\n' </code></pre> <p>The return value is a <code>bytes</code> object, so if you want a proper string, you'll need to <code>decode</code> it. Assuming the called process returns a UTF-8-encoded string:</p> <pre><code>&gt;&gt;&gt; result.stdout.decode('utf-8') 'total 0\n-rw-r--r-- 1 memyself staff 0 Mar 14 11:04 files\n' </code></pre> <p>This can all be compressed to a one-liner:</p> <pre><code>&gt;&gt;&gt; subprocess.run(['ls', '-l'], stdout=subprocess.PIPE).stdout.decode('utf-8') 'total 0\n-rw-r--r-- 1 memyself staff 0 Mar 14 11:04 files\n' </code></pre> <p>If you want to pass input to the process's <code>stdin</code>, pass a <code>bytes</code> object to the <code>input</code> keyword argument:</p> <pre><code>&gt;&gt;&gt; cmd = ['awk', 'length($0) &gt; 5'] &gt;&gt;&gt; input = 'foo\nfoofoo\n'.encode('utf-8') &gt;&gt;&gt; result = subprocess.run(cmd, stdout=subprocess.PIPE, input=input) &gt;&gt;&gt; result.stdout.decode('utf-8') 'foofoo\n' </code></pre> <p>You can capture errors by passing <code>stderr=subprocess.PIPE</code> (capture to <code>result.stderr</code>) or <code>stderr=subprocess.STDOUT</code> (capture to <code>result.stdout</code> along with regular output). When security is not a concern, you can also run more complex shell commands by passing <code>shell=True</code> as described in the notes below.</p> <p>This adds just a bit of complexity, compared to the old way of doing things. But I think it's worth the payoff: now you can do almost anything you need to do with the <code>run</code> function alone.</p> <h3>Older versions of Python (2.7-3.4): <code>check_output</code></h3> <p>If you are using an older version of Python, or need modest backwards compatibility, you can probably use the <code>check_output</code> function as briefly described above. It has been available since Python 2.7.</p> <pre><code>subprocess.check_output(*popenargs, **kwargs) </code></pre> <p>It takes takes the same arguments as <code>Popen</code> (see below), and returns a string containing the program's output. The beginning of this answer has a more detailed usage example. </p> <p>You can pass <code>stderr=subprocess.STDOUT</code> to ensure that error messages are included in the returned output -- but don't pass <code>stderr=subprocess.PIPE</code> to <code>check_output</code>. It can cause <a href="http://docs.python.org/library/subprocess.html#subprocess.check_output" rel="noreferrer">deadlocks</a>. When security is not a concern, you can also run more complex shell commands by passing <code>shell=True</code> as described in the notes below.</p> <p>If you need to pipe from <code>stderr</code> or pass input to the process, <code>check_output</code> won't be up to the task. See the <code>Popen</code> examples below in that case. </p> <h3>Complex applications &amp; legacy versions of Python (2.6 and below): <code>Popen</code></h3> <p>If you need deep backwards compatibility, or if you need more sophisticated functionality than <code>check_output</code> provides, you'll have to work directly with <code>Popen</code> objects, which encapsulate the low-level API for subprocesses. </p> <p>The <code>Popen</code> constructor accepts either <strong>a single command</strong> without arguments, or <strong>a list</strong> containing a command as its first item, followed by any number of arguments, each as a separate item in the list. <a href="https://docs.python.org/3/library/shlex.html" rel="noreferrer"><code>shlex.split</code></a> can help parse strings into appropriately formatted lists. <code>Popen</code> objects also accept a <a href="https://docs.python.org/3/library/subprocess.html#subprocess.Popen" rel="noreferrer">host of different arguments</a> for process IO management and low-level configuration. </p> <p>To send input and capture output, <code>communicate</code> is almost always the preferred method. As in:</p> <pre><code>output = subprocess.Popen(["mycmd", "myarg"], stdout=subprocess.PIPE).communicate()[0] </code></pre> <p>Or </p> <pre><code>&gt;&gt;&gt; import subprocess &gt;&gt;&gt; p = subprocess.Popen(['ls', '-a'], stdout=subprocess.PIPE, ... stderr=subprocess.PIPE) &gt;&gt;&gt; out, err = p.communicate() &gt;&gt;&gt; print out . .. foo </code></pre> <p>If you set <code>stdin=PIPE</code>, <code>communicate</code> also allows you to pass data to the process via <code>stdin</code>:</p> <pre><code>&gt;&gt;&gt; cmd = ['awk', 'length($0) &gt; 5'] &gt;&gt;&gt; p = subprocess.Popen(cmd, stdout=subprocess.PIPE, ... stderr=subprocess.PIPE, ... stdin=subprocess.PIPE) &gt;&gt;&gt; out, err = p.communicate('foo\nfoofoo\n') &gt;&gt;&gt; print out foofoo </code></pre> <p>Note <a href="https://stackoverflow.com/a/21867841/577088">Aaron Hall's answer</a>, which indicates that on some systems, you may need to set <code>stdout</code>, <code>stderr</code>, and <code>stdin</code> all to <code>PIPE</code> (or <code>DEVNULL</code>) to get <code>communicate</code> to work at all.</p> <p>In some rare cases, you may need complex, real-time output capturing. <a href="https://stackoverflow.com/a/4760274/577088">Vartec</a>'s answer suggests a way forward, but methods other than <code>communicate</code> are prone to deadlocks if not used carefully.</p> <p>As with all the above functions, when security is not a concern, you can run more complex shell commands by passing <code>shell=True</code>.</p> <h3>Notes</h3> <p><strong>1. Running shell commands: the <code>shell=True</code> argument</strong></p> <p>Normally, each call to <code>run</code>, <code>check_output</code>, or the <code>Popen</code> constructor executes a <em>single program</em>. That means no fancy bash-style pipes. If you want to run complex shell commands, you can pass <code>shell=True</code>, which all three functions support.</p> <p>However, doing so raises <a href="https://docs.python.org/3/library/subprocess.html#security-considerations" rel="noreferrer">security concerns</a>. If you're doing anything more than light scripting, you might be better off calling each process separately, and passing the output from each as an input to the next, via</p> <pre><code>run(cmd, [stdout=etc...], input=other_output) </code></pre> <p>Or</p> <pre><code>Popen(cmd, [stdout=etc...]).communicate(other_output) </code></pre> <p>The temptation to directly connect pipes is strong; resist it. Otherwise, you'll likely see deadlocks or have to do hacky things like <a href="https://stackoverflow.com/questions/6341451/piping-together-several-subprocesses">this</a>.</p> <p><strong>2. Unicode considerations</strong></p> <p><code>check_output</code> returns a string in Python 2, but a <code>bytes</code> object in Python 3. It's worth taking a moment to <a href="https://docs.python.org/3/howto/unicode.html" rel="noreferrer">learn about unicode</a> if you haven't already.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload