Sophie

Sophie

distrib > Mandriva > 2010.1 > x86_64 > media > contrib-backports > by-pkgid > 3ba3bd1608c672ba2129b098a48e9e4d > files > 807

python3-docs-3.2.2-3mdv2010.2.noarch.rpm



<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    
    <title>17.1. subprocess — Subprocess management &mdash; Python v3.2.2 documentation</title>
    <link rel="stylesheet" href="../_static/default.css" type="text/css" />
    <link rel="stylesheet" href="../_static/pygments.css" type="text/css" />
    <script type="text/javascript">
      var DOCUMENTATION_OPTIONS = {
        URL_ROOT:    '../',
        VERSION:     '3.2.2',
        COLLAPSE_INDEX: false,
        FILE_SUFFIX: '.html',
        HAS_SOURCE:  true
      };
    </script>
    <script type="text/javascript" src="../_static/jquery.js"></script>
    <script type="text/javascript" src="../_static/underscore.js"></script>
    <script type="text/javascript" src="../_static/doctools.js"></script>
    <script type="text/javascript" src="../_static/sidebar.js"></script>
    <link rel="search" type="application/opensearchdescription+xml"
          title="Search within Python v3.2.2 documentation"
          href="../_static/opensearch.xml"/>
    <link rel="author" title="About these documents" href="../about.html" />
    <link rel="copyright" title="Copyright" href="../copyright.html" />
    <link rel="top" title="Python v3.2.2 documentation" href="../index.html" />
    <link rel="up" title="17. Interprocess Communication and Networking" href="ipc.html" />
    <link rel="next" title="17.2. socket — Low-level networking interface" href="socket.html" />
    <link rel="prev" title="17. Interprocess Communication and Networking" href="ipc.html" />
    <link rel="shortcut icon" type="image/png" href="../_static/py.png" />
 

  </head>
  <body>
    <div class="related">
      <h3>Navigation</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="../genindex.html" title="General Index"
             accesskey="I">index</a></li>
        <li class="right" >
          <a href="../py-modindex.html" title="Python Module Index"
             >modules</a> |</li>
        <li class="right" >
          <a href="socket.html" title="17.2. socket — Low-level networking interface"
             accesskey="N">next</a> |</li>
        <li class="right" >
          <a href="ipc.html" title="17. Interprocess Communication and Networking"
             accesskey="P">previous</a> |</li>
        <li><img src="../_static/py.png" alt=""
                 style="vertical-align: middle; margin-top: -1px"/></li>
        <li><a href="../index.html">Python v3.2.2 documentation</a> &raquo;</li>

          <li><a href="index.html" >The Python Standard Library</a> &raquo;</li>
          <li><a href="ipc.html" accesskey="U">17. Interprocess Communication and Networking</a> &raquo;</li> 
      </ul>
    </div>  

    <div class="document">
      <div class="documentwrapper">
        <div class="bodywrapper">
          <div class="body">
            
  <div class="section" id="module-subprocess">
<span id="subprocess-subprocess-management"></span><h1>17.1. <a class="reference internal" href="#module-subprocess" title="subprocess: Subprocess management."><tt class="xref py py-mod docutils literal"><span class="pre">subprocess</span></tt></a> &#8212; Subprocess management<a class="headerlink" href="#module-subprocess" title="Permalink to this headline">¶</a></h1>
<p>The <a class="reference internal" href="#module-subprocess" title="subprocess: Subprocess management."><tt class="xref py py-mod docutils literal"><span class="pre">subprocess</span></tt></a> module allows you to spawn new processes, connect to their
input/output/error pipes, and obtain their return codes.  This module intends to
replace several other, older modules and functions, such as:</p>
<div class="highlight-python3"><div class="highlight"><pre><span class="n">os</span><span class="o">.</span><span class="n">system</span>
<span class="n">os</span><span class="o">.</span><span class="n">spawn</span><span class="o">*</span>
</pre></div>
</div>
<p>Information about how the <a class="reference internal" href="#module-subprocess" title="subprocess: Subprocess management."><tt class="xref py py-mod docutils literal"><span class="pre">subprocess</span></tt></a> module can be used to replace these
modules and functions can be found in the following sections.</p>
<div class="admonition-see-also admonition seealso">
<p class="first admonition-title">See also</p>
<p class="last"><span class="target" id="index-0"></span><a class="pep reference external" href="http://www.python.org/dev/peps/pep-0324"><strong>PEP 324</strong></a> &#8211; PEP proposing the subprocess module</p>
</div>
<div class="section" id="using-the-subprocess-module">
<h2>17.1.1. Using the subprocess Module<a class="headerlink" href="#using-the-subprocess-module" title="Permalink to this headline">¶</a></h2>
<p>This module defines one class called <a class="reference internal" href="#subprocess.Popen" title="subprocess.Popen"><tt class="xref py py-class docutils literal"><span class="pre">Popen</span></tt></a>:</p>
<dl class="class">
<dt id="subprocess.Popen">
<em class="property">class </em><tt class="descclassname">subprocess.</tt><tt class="descname">Popen</tt><big>(</big><em>args</em>, <em>bufsize=0</em>, <em>executable=None</em>, <em>stdin=None</em>, <em>stdout=None</em>, <em>stderr=None</em>, <em>preexec_fn=None</em>, <em>close_fds=True</em>, <em>shell=False</em>, <em>cwd=None</em>, <em>env=None</em>, <em>universal_newlines=False</em>, <em>startupinfo=None</em>, <em>creationflags=0</em>, <em>restore_signals=True</em>, <em>start_new_session=False</em>, <em>pass_fds=()</em><big>)</big><a class="headerlink" href="#subprocess.Popen" title="Permalink to this definition">¶</a></dt>
<dd><p>Arguments are:</p>
<p><em>args</em> should be a string, or a sequence of program arguments.  The program
to execute is normally the first item in the args sequence or the string if
a string is given, but can be explicitly set by using the <em>executable</em>
argument.  When <em>executable</em> is given, the first item in the args sequence
is still treated by most programs as the command name, which can then be
different from the actual executable name.  On Unix, it becomes the display
name for the executing program in utilities such as <strong class="program">ps</strong>.</p>
<p>On Unix, with <em>shell=False</em> (default): In this case, the Popen class uses
<a class="reference internal" href="os.html#os.execvp" title="os.execvp"><tt class="xref py py-meth docutils literal"><span class="pre">os.execvp()</span></tt></a> like behavior to execute the child program.
<em>args</em> should normally be a
sequence.  If a string is specified for <em>args</em>, it will be used as the name
or path of the program to execute; this will only work if the program is
being given no arguments.</p>
<div class="admonition note">
<p class="first admonition-title">Note</p>
<p><a class="reference internal" href="shlex.html#shlex.split" title="shlex.split"><tt class="xref py py-meth docutils literal"><span class="pre">shlex.split()</span></tt></a> can be useful when determining the correct
tokenization for <em>args</em>, especially in complex cases:</p>
<div class="highlight-python3"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="kn">import</span> <span class="nn">shlex</span><span class="o">,</span> <span class="nn">subprocess</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">command_line</span> <span class="o">=</span> <span class="nb">input</span><span class="p">()</span>
<span class="go">/bin/vikings -input eggs.txt -output &quot;spam spam.txt&quot; -cmd &quot;echo &#39;$MONEY&#39;&quot;</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">args</span> <span class="o">=</span> <span class="n">shlex</span><span class="o">.</span><span class="n">split</span><span class="p">(</span><span class="n">command_line</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="nb">print</span><span class="p">(</span><span class="n">args</span><span class="p">)</span>
<span class="go">[&#39;/bin/vikings&#39;, &#39;-input&#39;, &#39;eggs.txt&#39;, &#39;-output&#39;, &#39;spam spam.txt&#39;, &#39;-cmd&#39;, &quot;echo &#39;$MONEY&#39;&quot;]</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">p</span> <span class="o">=</span> <span class="n">subprocess</span><span class="o">.</span><span class="n">Popen</span><span class="p">(</span><span class="n">args</span><span class="p">)</span> <span class="c"># Success!</span>
</pre></div>
</div>
<p class="last">Note in particular that options (such as <em>-input</em>) and arguments (such
as <em>eggs.txt</em>) that are separated by whitespace in the shell go in separate
list elements, while arguments that need quoting or backslash escaping when
used in the shell (such as filenames containing spaces or the <em>echo</em> command
shown above) are single list elements.</p>
</div>
<p>On Unix, with <em>shell=True</em>: If args is a string, it specifies the command
string to execute through the shell.  This means that the string must be
formatted exactly as it would be when typed at the shell prompt.  This
includes, for example, quoting or backslash escaping filenames with spaces in
them.  If <em>args</em> is a sequence, the first item specifies the command string, and
any additional items will be treated as additional arguments to the shell
itself.  That is to say, <em>Popen</em> does the equivalent of:</p>
<div class="highlight-python3"><div class="highlight"><pre><span class="n">Popen</span><span class="p">([</span><span class="s">&#39;/bin/sh&#39;</span><span class="p">,</span> <span class="s">&#39;-c&#39;</span><span class="p">,</span> <span class="n">args</span><span class="p">[</span><span class="mi">0</span><span class="p">],</span> <span class="n">args</span><span class="p">[</span><span class="mi">1</span><span class="p">],</span> <span class="o">...</span><span class="p">])</span>
</pre></div>
</div>
<div class="admonition warning">
<p class="first admonition-title">Warning</p>
<p>Executing shell commands that incorporate unsanitized input from an
untrusted source makes a program vulnerable to <a class="reference external" href="http://en.wikipedia.org/wiki/Shell_injection#Shell_injection">shell injection</a>,
a serious security flaw which can result in arbitrary command execution.
For this reason, the use of <em>shell=True</em> is <strong>strongly discouraged</strong> in cases
where the command string is constructed from external input:</p>
<div class="highlight-python3"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">subprocess</span> <span class="k">import</span> <span class="n">call</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">filename</span> <span class="o">=</span> <span class="nb">input</span><span class="p">(</span><span class="s">&quot;What file would you like to display?</span><span class="se">\n</span><span class="s">&quot;</span><span class="p">)</span>
<span class="go">What file would you like to display?</span>
<span class="go">non_existent; rm -rf / #</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">call</span><span class="p">(</span><span class="s">&quot;cat &quot;</span> <span class="o">+</span> <span class="n">filename</span><span class="p">,</span> <span class="n">shell</span><span class="o">=</span><span class="k">True</span><span class="p">)</span> <span class="c"># Uh-oh. This will end badly...</span>
</pre></div>
</div>
<p class="last"><em>shell=False</em> does not suffer from this vulnerability; the above Note may be
helpful in getting code using <em>shell=False</em> to work.</p>
</div>
<p>On Windows: the <a class="reference internal" href="#subprocess.Popen" title="subprocess.Popen"><tt class="xref py py-class docutils literal"><span class="pre">Popen</span></tt></a> class uses CreateProcess() to execute the
child program, which operates on strings.  If <em>args</em> is a sequence, it will
be converted to a string in a manner described in
<a class="reference internal" href="#converting-argument-sequence"><em>Converting an argument sequence to a string on Windows</em></a>.</p>
<p><em>bufsize</em>, if given, has the same meaning as the corresponding argument to the
built-in open() function: <tt class="xref py py-const docutils literal"><span class="pre">0</span></tt> means unbuffered, <tt class="xref py py-const docutils literal"><span class="pre">1</span></tt> means line
buffered, any other positive value means use a buffer of (approximately) that
size.  A negative <em>bufsize</em> means to use the system default, which usually means
fully buffered.  The default value for <em>bufsize</em> is <tt class="xref py py-const docutils literal"><span class="pre">0</span></tt> (unbuffered).</p>
<div class="admonition note">
<p class="first admonition-title">Note</p>
<p class="last">If you experience performance issues, it is recommended that you try to
enable buffering by setting <em>bufsize</em> to either -1 or a large enough
positive value (such as 4096).</p>
</div>
<p>The <em>executable</em> argument specifies the program to execute. It is very seldom
needed: Usually, the program to execute is defined by the <em>args</em> argument. If
<tt class="docutils literal"><span class="pre">shell=True</span></tt>, the <em>executable</em> argument specifies which shell to use. On Unix,
the default shell is <tt class="file docutils literal"><span class="pre">/bin/sh</span></tt>.  On Windows, the default shell is
specified by the <span class="target" id="index-1"></span><tt class="xref std std-envvar docutils literal"><span class="pre">COMSPEC</span></tt> environment variable. The only reason you
would need to specify <tt class="docutils literal"><span class="pre">shell=True</span></tt> on Windows is where the command you
wish to execute is actually built in to the shell, eg <tt class="docutils literal"><span class="pre">dir</span></tt>, <tt class="docutils literal"><span class="pre">copy</span></tt>.
You don&#8217;t need <tt class="docutils literal"><span class="pre">shell=True</span></tt> to run a batch file, nor to run a console-based
executable.</p>
<p><em>stdin</em>, <em>stdout</em> and <em>stderr</em> specify the executed programs&#8217; standard input,
standard output and standard error file handles, respectively.  Valid values
are <a class="reference internal" href="#subprocess.PIPE" title="subprocess.PIPE"><tt class="xref py py-data docutils literal"><span class="pre">PIPE</span></tt></a>, an existing file descriptor (a positive integer), an
existing <a class="reference internal" href="../glossary.html#term-file-object"><em class="xref std std-term">file object</em></a>, and <tt class="xref docutils literal"><span class="pre">None</span></tt>.  <a class="reference internal" href="#subprocess.PIPE" title="subprocess.PIPE"><tt class="xref py py-data docutils literal"><span class="pre">PIPE</span></tt></a> indicates that a
new pipe to the child should be created.  With <tt class="xref docutils literal"><span class="pre">None</span></tt>, no redirection will
occur; the child&#8217;s file handles will be inherited from the parent.  Additionally,
<em>stderr</em> can be <a class="reference internal" href="#subprocess.STDOUT" title="subprocess.STDOUT"><tt class="xref py py-data docutils literal"><span class="pre">STDOUT</span></tt></a>, which indicates that the stderr data from the
applications should be captured into the same file handle as for stdout.</p>
<p>If <em>preexec_fn</em> is set to a callable object, this object will be called in the
child process just before the child is executed.
(Unix only)</p>
<div class="admonition warning">
<p class="first admonition-title">Warning</p>
<p class="last">The <em>preexec_fn</em> parameter is not safe to use in the presence of threads
in your application.  The child process could deadlock before exec is
called.
If you must use it, keep it trivial!  Minimize the number of libraries
you call into.</p>
</div>
<div class="admonition note">
<p class="first admonition-title">Note</p>
<p class="last">If you need to modify the environment for the child use the <em>env</em>
parameter rather than doing it in a <em>preexec_fn</em>.
The <em>start_new_session</em> parameter can take the place of a previously
common use of <em>preexec_fn</em> to call os.setsid() in the child.</p>
</div>
<p>If <em>close_fds</em> is true, all file descriptors except <tt class="xref py py-const docutils literal"><span class="pre">0</span></tt>, <tt class="xref py py-const docutils literal"><span class="pre">1</span></tt> and
<tt class="xref py py-const docutils literal"><span class="pre">2</span></tt> will be closed before the child process is executed. (Unix only).
The default varies by platform:  Always true on Unix.  On Windows it is
true when <em>stdin</em>/<em>stdout</em>/<em>stderr</em> are <a class="reference internal" href="constants.html#None" title="None"><tt class="xref py py-const xref docutils literal"><span class="pre">None</span></tt></a>, false otherwise.
On Windows, if <em>close_fds</em> is true then no handles will be inherited by the
child process.  Note that on Windows, you cannot set <em>close_fds</em> to true and
also redirect the standard handles by setting <em>stdin</em>, <em>stdout</em> or <em>stderr</em>.</p>
<p class="versionchanged">
<span class="versionmodified">Changed in version 3.2: </span>The default for <em>close_fds</em> was changed from <a class="reference internal" href="constants.html#False" title="False"><tt class="xref py py-const xref docutils literal"><span class="pre">False</span></tt></a> to
what is described above.</p>
<p><em>pass_fds</em> is an optional sequence of file descriptors to keep open
between the parent and child.  Providing any <em>pass_fds</em> forces
<em>close_fds</em> to be <a class="reference internal" href="constants.html#True" title="True"><tt class="xref py py-const xref docutils literal"><span class="pre">True</span></tt></a>.  (Unix only)</p>
<p class="versionadded">
<span class="versionmodified">New in version 3.2: </span>The <em>pass_fds</em> parameter was added.</p>
<p>If <em>cwd</em> is not <tt class="xref docutils literal"><span class="pre">None</span></tt>, the child&#8217;s current directory will be changed to <em>cwd</em>
before it is executed.  Note that this directory is not considered when
searching the executable, so you can&#8217;t specify the program&#8217;s path relative to
<em>cwd</em>.</p>
<p>If <em>restore_signals</em> is True (the default) all signals that Python has set to
SIG_IGN are restored to SIG_DFL in the child process before the exec.
Currently this includes the SIGPIPE, SIGXFZ and SIGXFSZ signals.
(Unix only)</p>
<p class="versionchanged">
<span class="versionmodified">Changed in version 3.2: </span><em>restore_signals</em> was added.</p>
<p>If <em>start_new_session</em> is True the setsid() system call will be made in the
child process prior to the execution of the subprocess.  (Unix only)</p>
<p class="versionchanged">
<span class="versionmodified">Changed in version 3.2: </span><em>start_new_session</em> was added.</p>
<p>If <em>env</em> is not <tt class="xref docutils literal"><span class="pre">None</span></tt>, it must be a mapping that defines the environment
variables for the new process; these are used instead of the default
behavior of inheriting the current process&#8217; environment.</p>
<div class="admonition note">
<p class="first admonition-title">Note</p>
<p class="last">If specified, <em>env</em> must provide any variables required for the program to
execute.  On Windows, in order to run a <a class="reference external" href="http://en.wikipedia.org/wiki/Side-by-Side_Assembly">side-by-side assembly</a> the
specified <em>env</em> <strong>must</strong> include a valid <span class="target" id="index-2"></span><tt class="xref std std-envvar docutils literal"><span class="pre">SystemRoot</span></tt>.</p>
</div>
<p>If <em>universal_newlines</em> is <a class="reference internal" href="constants.html#True" title="True"><tt class="xref py py-const xref docutils literal"><span class="pre">True</span></tt></a>, the file objects stdout and stderr are
opened as text files, but lines may be terminated by any of <tt class="docutils literal"><span class="pre">'\n'</span></tt>, the Unix
end-of-line convention, <tt class="docutils literal"><span class="pre">'\r'</span></tt>, the old Macintosh convention or <tt class="docutils literal"><span class="pre">'\r\n'</span></tt>, the
Windows convention. All of these external representations are seen as <tt class="docutils literal"><span class="pre">'\n'</span></tt>
by the Python program.</p>
<div class="admonition note">
<p class="first admonition-title">Note</p>
<p class="last">This feature is only available if Python is built with universal newline
support (the default).  Also, the newlines attribute of the file objects
<a class="reference internal" href="#subprocess.Popen.stdout" title="subprocess.Popen.stdout"><tt class="xref py py-attr docutils literal"><span class="pre">stdout</span></tt></a>, <a class="reference internal" href="#subprocess.Popen.stdin" title="subprocess.Popen.stdin"><tt class="xref py py-attr docutils literal"><span class="pre">stdin</span></tt></a> and <a class="reference internal" href="#subprocess.Popen.stderr" title="subprocess.Popen.stderr"><tt class="xref py py-attr docutils literal"><span class="pre">stderr</span></tt></a> are not updated by the
<a class="reference internal" href="#subprocess.Popen.communicate" title="subprocess.Popen.communicate"><tt class="xref py py-meth docutils literal"><span class="pre">communicate()</span></tt></a> method.</p>
</div>
<p>If given, <em>startupinfo</em> will be a <a class="reference internal" href="#subprocess.STARTUPINFO" title="subprocess.STARTUPINFO"><tt class="xref py py-class docutils literal"><span class="pre">STARTUPINFO</span></tt></a> object, which is
passed to the underlying <tt class="docutils literal"><span class="pre">CreateProcess</span></tt> function.
<em>creationflags</em>, if given, can be <a class="reference internal" href="#subprocess.CREATE_NEW_CONSOLE" title="subprocess.CREATE_NEW_CONSOLE"><tt class="xref py py-data docutils literal"><span class="pre">CREATE_NEW_CONSOLE</span></tt></a> or
<a class="reference internal" href="#subprocess.CREATE_NEW_PROCESS_GROUP" title="subprocess.CREATE_NEW_PROCESS_GROUP"><tt class="xref py py-data docutils literal"><span class="pre">CREATE_NEW_PROCESS_GROUP</span></tt></a>. (Windows only)</p>
<p>Popen objects are supported as context managers via the <a class="reference internal" href="../reference/compound_stmts.html#with"><tt class="xref std std-keyword docutils literal"><span class="pre">with</span></tt></a> statement:
on exit, standard file descriptors are closed, and the process is waited for.</p>
<div class="highlight-python3"><div class="highlight"><pre><span class="k">with</span> <span class="n">Popen</span><span class="p">([</span><span class="s">&quot;ifconfig&quot;</span><span class="p">],</span> <span class="n">stdout</span><span class="o">=</span><span class="n">PIPE</span><span class="p">)</span> <span class="k">as</span> <span class="n">proc</span><span class="p">:</span>
    <span class="n">log</span><span class="o">.</span><span class="n">write</span><span class="p">(</span><span class="n">proc</span><span class="o">.</span><span class="n">stdout</span><span class="o">.</span><span class="n">read</span><span class="p">())</span>
</pre></div>
</div>
<p class="versionchanged">
<span class="versionmodified">Changed in version 3.2: </span>Added context manager support.</p>
</dd></dl>

<dl class="data">
<dt id="subprocess.PIPE">
<tt class="descclassname">subprocess.</tt><tt class="descname">PIPE</tt><a class="headerlink" href="#subprocess.PIPE" title="Permalink to this definition">¶</a></dt>
<dd><p>Special value that can be used as the <em>stdin</em>, <em>stdout</em> or <em>stderr</em> argument
to <a class="reference internal" href="#subprocess.Popen" title="subprocess.Popen"><tt class="xref py py-class docutils literal"><span class="pre">Popen</span></tt></a> and indicates that a pipe to the standard stream should be
opened.</p>
</dd></dl>

<dl class="data">
<dt id="subprocess.STDOUT">
<tt class="descclassname">subprocess.</tt><tt class="descname">STDOUT</tt><a class="headerlink" href="#subprocess.STDOUT" title="Permalink to this definition">¶</a></dt>
<dd><p>Special value that can be used as the <em>stderr</em> argument to <a class="reference internal" href="#subprocess.Popen" title="subprocess.Popen"><tt class="xref py py-class docutils literal"><span class="pre">Popen</span></tt></a> and
indicates that standard error should go into the same handle as standard
output.</p>
</dd></dl>

<div class="section" id="convenience-functions">
<h3>17.1.1.1. Convenience Functions<a class="headerlink" href="#convenience-functions" title="Permalink to this headline">¶</a></h3>
<p>This module also defines the following shortcut functions:</p>
<dl class="function">
<dt id="subprocess.call">
<tt class="descclassname">subprocess.</tt><tt class="descname">call</tt><big>(</big><em>*popenargs</em>, <em>**kwargs</em><big>)</big><a class="headerlink" href="#subprocess.call" title="Permalink to this definition">¶</a></dt>
<dd><p>Run command with arguments.  Wait for command to complete, then return the
<tt class="xref py py-attr docutils literal"><span class="pre">returncode</span></tt> attribute.</p>
<p>The arguments are the same as for the <a class="reference internal" href="#subprocess.Popen" title="subprocess.Popen"><tt class="xref py py-class docutils literal"><span class="pre">Popen</span></tt></a> constructor.  Example:</p>
<div class="highlight-python3"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="n">retcode</span> <span class="o">=</span> <span class="n">subprocess</span><span class="o">.</span><span class="n">call</span><span class="p">([</span><span class="s">&quot;ls&quot;</span><span class="p">,</span> <span class="s">&quot;-l&quot;</span><span class="p">])</span>
</pre></div>
</div>
<div class="admonition warning">
<p class="first admonition-title">Warning</p>
<p class="last">Like <a class="reference internal" href="#subprocess.Popen.wait" title="subprocess.Popen.wait"><tt class="xref py py-meth docutils literal"><span class="pre">Popen.wait()</span></tt></a>, this will deadlock when using
<tt class="docutils literal"><span class="pre">stdout=PIPE</span></tt> and/or <tt class="docutils literal"><span class="pre">stderr=PIPE</span></tt> and the child process
generates enough output to a pipe such that it blocks waiting
for the OS pipe buffer to accept more data.</p>
</div>
</dd></dl>

<dl class="function">
<dt id="subprocess.check_call">
<tt class="descclassname">subprocess.</tt><tt class="descname">check_call</tt><big>(</big><em>*popenargs</em>, <em>**kwargs</em><big>)</big><a class="headerlink" href="#subprocess.check_call" title="Permalink to this definition">¶</a></dt>
<dd><p>Run command with arguments.  Wait for command to complete. If the exit code was
zero then return, otherwise raise <tt class="xref py py-exc docutils literal"><span class="pre">CalledProcessError</span></tt>. The
<tt class="xref py py-exc docutils literal"><span class="pre">CalledProcessError</span></tt> object will have the return code in the
<tt class="xref py py-attr docutils literal"><span class="pre">returncode</span></tt> attribute.</p>
<p>The arguments are the same as for the <a class="reference internal" href="#subprocess.Popen" title="subprocess.Popen"><tt class="xref py py-class docutils literal"><span class="pre">Popen</span></tt></a> constructor.  Example:</p>
<div class="highlight-python3"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="n">subprocess</span><span class="o">.</span><span class="n">check_call</span><span class="p">([</span><span class="s">&quot;ls&quot;</span><span class="p">,</span> <span class="s">&quot;-l&quot;</span><span class="p">])</span>
<span class="go">0</span>
</pre></div>
</div>
<div class="admonition warning">
<p class="first admonition-title">Warning</p>
<p class="last">See the warning for <a class="reference internal" href="#subprocess.call" title="subprocess.call"><tt class="xref py py-func docutils literal"><span class="pre">call()</span></tt></a>.</p>
</div>
</dd></dl>

<dl class="function">
<dt id="subprocess.check_output">
<tt class="descclassname">subprocess.</tt><tt class="descname">check_output</tt><big>(</big><em>*popenargs</em>, <em>**kwargs</em><big>)</big><a class="headerlink" href="#subprocess.check_output" title="Permalink to this definition">¶</a></dt>
<dd><p>Run command with arguments and return its output as a byte string.</p>
<p>If the exit code was non-zero it raises a <tt class="xref py py-exc docutils literal"><span class="pre">CalledProcessError</span></tt>.  The
<tt class="xref py py-exc docutils literal"><span class="pre">CalledProcessError</span></tt> object will have the return code in the
<tt class="xref py py-attr docutils literal"><span class="pre">returncode</span></tt>
attribute and output in the <tt class="xref py py-attr docutils literal"><span class="pre">output</span></tt> attribute.</p>
<p>The arguments are the same as for the <a class="reference internal" href="#subprocess.Popen" title="subprocess.Popen"><tt class="xref py py-class docutils literal"><span class="pre">Popen</span></tt></a> constructor.  Example:</p>
<div class="highlight-python3"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="n">subprocess</span><span class="o">.</span><span class="n">check_output</span><span class="p">([</span><span class="s">&quot;ls&quot;</span><span class="p">,</span> <span class="s">&quot;-l&quot;</span><span class="p">,</span> <span class="s">&quot;/dev/null&quot;</span><span class="p">])</span>
<span class="go">b&#39;crw-rw-rw- 1 root root 1, 3 Oct 18  2007 /dev/null\n&#39;</span>
</pre></div>
</div>
<p>The stdout argument is not allowed as it is used internally.
To capture standard error in the result, use <tt class="docutils literal"><span class="pre">stderr=subprocess.STDOUT</span></tt>:</p>
<div class="highlight-python3"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="n">subprocess</span><span class="o">.</span><span class="n">check_output</span><span class="p">(</span>
<span class="gp">... </span>    <span class="p">[</span><span class="s">&quot;/bin/sh&quot;</span><span class="p">,</span> <span class="s">&quot;-c&quot;</span><span class="p">,</span> <span class="s">&quot;ls non_existent_file; exit 0&quot;</span><span class="p">],</span>
<span class="gp">... </span>    <span class="n">stderr</span><span class="o">=</span><span class="n">subprocess</span><span class="o">.</span><span class="n">STDOUT</span><span class="p">)</span>
<span class="go">b&#39;ls: non_existent_file: No such file or directory\n&#39;</span>
</pre></div>
</div>
<p class="versionadded">
<span class="versionmodified">New in version 3.1.</span></p>
</dd></dl>

<dl class="function">
<dt id="subprocess.getstatusoutput">
<tt class="descclassname">subprocess.</tt><tt class="descname">getstatusoutput</tt><big>(</big><em>cmd</em><big>)</big><a class="headerlink" href="#subprocess.getstatusoutput" title="Permalink to this definition">¶</a></dt>
<dd><p>Return <tt class="docutils literal"><span class="pre">(status,</span> <span class="pre">output)</span></tt> of executing <em>cmd</em> in a shell.</p>
<p>Execute the string <em>cmd</em> in a shell with <tt class="xref py py-func docutils literal"><span class="pre">os.popen()</span></tt> and return a 2-tuple
<tt class="docutils literal"><span class="pre">(status,</span> <span class="pre">output)</span></tt>.  <em>cmd</em> is actually run as <tt class="docutils literal"><span class="pre">{</span> <span class="pre">cmd</span> <span class="pre">;</span> <span class="pre">}</span> <span class="pre">2&gt;&amp;1</span></tt>, so that the
returned output will contain output or error messages.  A trailing newline is
stripped from the output.  The exit status for the command can be interpreted
according to the rules for the C function <tt class="xref c c-func docutils literal"><span class="pre">wait()</span></tt>.  Example:</p>
<div class="highlight-python3"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="n">subprocess</span><span class="o">.</span><span class="n">getstatusoutput</span><span class="p">(</span><span class="s">&#39;ls /bin/ls&#39;</span><span class="p">)</span>
<span class="go">(0, &#39;/bin/ls&#39;)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">subprocess</span><span class="o">.</span><span class="n">getstatusoutput</span><span class="p">(</span><span class="s">&#39;cat /bin/junk&#39;</span><span class="p">)</span>
<span class="go">(256, &#39;cat: /bin/junk: No such file or directory&#39;)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">subprocess</span><span class="o">.</span><span class="n">getstatusoutput</span><span class="p">(</span><span class="s">&#39;/bin/junk&#39;</span><span class="p">)</span>
<span class="go">(256, &#39;sh: /bin/junk: not found&#39;)</span>
</pre></div>
</div>
<p>Availability: UNIX.</p>
</dd></dl>

<dl class="function">
<dt id="subprocess.getoutput">
<tt class="descclassname">subprocess.</tt><tt class="descname">getoutput</tt><big>(</big><em>cmd</em><big>)</big><a class="headerlink" href="#subprocess.getoutput" title="Permalink to this definition">¶</a></dt>
<dd><p>Return output (stdout and stderr) of executing <em>cmd</em> in a shell.</p>
<p>Like <a class="reference internal" href="#subprocess.getstatusoutput" title="subprocess.getstatusoutput"><tt class="xref py py-func docutils literal"><span class="pre">getstatusoutput()</span></tt></a>, except the exit status is ignored and the return
value is a string containing the command&#8217;s output.  Example:</p>
<div class="highlight-python3"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="n">subprocess</span><span class="o">.</span><span class="n">getoutput</span><span class="p">(</span><span class="s">&#39;ls /bin/ls&#39;</span><span class="p">)</span>
<span class="go">&#39;/bin/ls&#39;</span>
</pre></div>
</div>
<p>Availability: UNIX.</p>
</dd></dl>

</div>
<div class="section" id="exceptions">
<h3>17.1.1.2. Exceptions<a class="headerlink" href="#exceptions" title="Permalink to this headline">¶</a></h3>
<p>Exceptions raised in the child process, before the new program has started to
execute, will be re-raised in the parent.  Additionally, the exception object
will have one extra attribute called <tt class="xref py py-attr docutils literal"><span class="pre">child_traceback</span></tt>, which is a string
containing traceback information from the child&#8217;s point of view.</p>
<p>The most common exception raised is <a class="reference internal" href="exceptions.html#OSError" title="OSError"><tt class="xref py py-exc docutils literal"><span class="pre">OSError</span></tt></a>.  This occurs, for example,
when trying to execute a non-existent file.  Applications should prepare for
<a class="reference internal" href="exceptions.html#OSError" title="OSError"><tt class="xref py py-exc docutils literal"><span class="pre">OSError</span></tt></a> exceptions.</p>
<p>A <a class="reference internal" href="exceptions.html#ValueError" title="ValueError"><tt class="xref py py-exc docutils literal"><span class="pre">ValueError</span></tt></a> will be raised if <a class="reference internal" href="#subprocess.Popen" title="subprocess.Popen"><tt class="xref py py-class docutils literal"><span class="pre">Popen</span></tt></a> is called with invalid
arguments.</p>
<p>check_call() will raise <tt class="xref py py-exc docutils literal"><span class="pre">CalledProcessError</span></tt>, if the called process returns
a non-zero return code.</p>
</div>
<div class="section" id="security">
<h3>17.1.1.3. Security<a class="headerlink" href="#security" title="Permalink to this headline">¶</a></h3>
<p>Unlike some other popen functions, this implementation will never call /bin/sh
implicitly.  This means that all characters, including shell metacharacters, can
safely be passed to child processes.</p>
</div>
</div>
<div class="section" id="popen-objects">
<h2>17.1.2. Popen Objects<a class="headerlink" href="#popen-objects" title="Permalink to this headline">¶</a></h2>
<p>Instances of the <a class="reference internal" href="#subprocess.Popen" title="subprocess.Popen"><tt class="xref py py-class docutils literal"><span class="pre">Popen</span></tt></a> class have the following methods:</p>
<dl class="method">
<dt id="subprocess.Popen.poll">
<tt class="descclassname">Popen.</tt><tt class="descname">poll</tt><big>(</big><big>)</big><a class="headerlink" href="#subprocess.Popen.poll" title="Permalink to this definition">¶</a></dt>
<dd><p>Check if child process has terminated.  Set and return <a class="reference internal" href="#subprocess.Popen.returncode" title="subprocess.Popen.returncode"><tt class="xref py py-attr docutils literal"><span class="pre">returncode</span></tt></a>
attribute.</p>
</dd></dl>

<dl class="method">
<dt id="subprocess.Popen.wait">
<tt class="descclassname">Popen.</tt><tt class="descname">wait</tt><big>(</big><big>)</big><a class="headerlink" href="#subprocess.Popen.wait" title="Permalink to this definition">¶</a></dt>
<dd><p>Wait for child process to terminate.  Set and return <a class="reference internal" href="#subprocess.Popen.returncode" title="subprocess.Popen.returncode"><tt class="xref py py-attr docutils literal"><span class="pre">returncode</span></tt></a>
attribute.</p>
<div class="admonition warning">
<p class="first admonition-title">Warning</p>
<p class="last">This will deadlock when using <tt class="docutils literal"><span class="pre">stdout=PIPE</span></tt> and/or
<tt class="docutils literal"><span class="pre">stderr=PIPE</span></tt> and the child process generates enough output to
a pipe such that it blocks waiting for the OS pipe buffer to
accept more data.  Use <a class="reference internal" href="#subprocess.Popen.communicate" title="subprocess.Popen.communicate"><tt class="xref py py-meth docutils literal"><span class="pre">communicate()</span></tt></a> to avoid that.</p>
</div>
</dd></dl>

<dl class="method">
<dt id="subprocess.Popen.communicate">
<tt class="descclassname">Popen.</tt><tt class="descname">communicate</tt><big>(</big><em>input=None</em><big>)</big><a class="headerlink" href="#subprocess.Popen.communicate" title="Permalink to this definition">¶</a></dt>
<dd><p>Interact with process: Send data to stdin.  Read data from stdout and stderr,
until end-of-file is reached.  Wait for process to terminate. The optional
<em>input</em> argument should be a byte string to be sent to the child process, or
<tt class="xref docutils literal"><span class="pre">None</span></tt>, if no data should be sent to the child.</p>
<p><a class="reference internal" href="#subprocess.Popen.communicate" title="subprocess.Popen.communicate"><tt class="xref py py-meth docutils literal"><span class="pre">communicate()</span></tt></a> returns a tuple <tt class="docutils literal"><span class="pre">(stdoutdata,</span> <span class="pre">stderrdata)</span></tt>.</p>
<p>Note that if you want to send data to the process&#8217;s stdin, you need to create
the Popen object with <tt class="docutils literal"><span class="pre">stdin=PIPE</span></tt>.  Similarly, to get anything other than
<tt class="xref docutils literal"><span class="pre">None</span></tt> in the result tuple, you need to give <tt class="docutils literal"><span class="pre">stdout=PIPE</span></tt> and/or
<tt class="docutils literal"><span class="pre">stderr=PIPE</span></tt> too.</p>
<div class="admonition note">
<p class="first admonition-title">Note</p>
<p class="last">The data read is buffered in memory, so do not use this method if the data
size is large or unlimited.</p>
</div>
</dd></dl>

<dl class="method">
<dt id="subprocess.Popen.send_signal">
<tt class="descclassname">Popen.</tt><tt class="descname">send_signal</tt><big>(</big><em>signal</em><big>)</big><a class="headerlink" href="#subprocess.Popen.send_signal" title="Permalink to this definition">¶</a></dt>
<dd><p>Sends the signal <em>signal</em> to the child.</p>
<div class="admonition note">
<p class="first admonition-title">Note</p>
<p class="last">On Windows, SIGTERM is an alias for <a class="reference internal" href="#subprocess.Popen.terminate" title="subprocess.Popen.terminate"><tt class="xref py py-meth docutils literal"><span class="pre">terminate()</span></tt></a>. CTRL_C_EVENT and
CTRL_BREAK_EVENT can be sent to processes started with a <em>creationflags</em>
parameter which includes <cite>CREATE_NEW_PROCESS_GROUP</cite>.</p>
</div>
</dd></dl>

<dl class="method">
<dt id="subprocess.Popen.terminate">
<tt class="descclassname">Popen.</tt><tt class="descname">terminate</tt><big>(</big><big>)</big><a class="headerlink" href="#subprocess.Popen.terminate" title="Permalink to this definition">¶</a></dt>
<dd><p>Stop the child. On Posix OSs the method sends SIGTERM to the
child. On Windows the Win32 API function <tt class="xref c c-func docutils literal"><span class="pre">TerminateProcess()</span></tt> is called
to stop the child.</p>
</dd></dl>

<dl class="method">
<dt id="subprocess.Popen.kill">
<tt class="descclassname">Popen.</tt><tt class="descname">kill</tt><big>(</big><big>)</big><a class="headerlink" href="#subprocess.Popen.kill" title="Permalink to this definition">¶</a></dt>
<dd><p>Kills the child. On Posix OSs the function sends SIGKILL to the child.
On Windows <a class="reference internal" href="#subprocess.Popen.kill" title="subprocess.Popen.kill"><tt class="xref py py-meth docutils literal"><span class="pre">kill()</span></tt></a> is an alias for <a class="reference internal" href="#subprocess.Popen.terminate" title="subprocess.Popen.terminate"><tt class="xref py py-meth docutils literal"><span class="pre">terminate()</span></tt></a>.</p>
</dd></dl>

<p>The following attributes are also available:</p>
<div class="admonition warning">
<p class="first admonition-title">Warning</p>
<p class="last">Use <tt class="xref py py-meth docutils literal"><span class="pre">communicate()</span></tt> rather than <tt class="xref py py-attr docutils literal"><span class="pre">.stdin.write</span></tt>,
<tt class="xref py py-attr docutils literal"><span class="pre">.stdout.read</span></tt> or <tt class="xref py py-attr docutils literal"><span class="pre">.stderr.read</span></tt> to avoid
deadlocks due to any of the other OS pipe buffers filling up and blocking the
child process.</p>
</div>
<dl class="attribute">
<dt id="subprocess.Popen.stdin">
<tt class="descclassname">Popen.</tt><tt class="descname">stdin</tt><a class="headerlink" href="#subprocess.Popen.stdin" title="Permalink to this definition">¶</a></dt>
<dd><p>If the <em>stdin</em> argument was <a class="reference internal" href="#subprocess.PIPE" title="subprocess.PIPE"><tt class="xref py py-data docutils literal"><span class="pre">PIPE</span></tt></a>, this attribute is a <a class="reference internal" href="../glossary.html#term-file-object"><em class="xref std std-term">file
object</em></a> that provides input to the child process.  Otherwise, it is <tt class="xref docutils literal"><span class="pre">None</span></tt>.</p>
</dd></dl>

<dl class="attribute">
<dt id="subprocess.Popen.stdout">
<tt class="descclassname">Popen.</tt><tt class="descname">stdout</tt><a class="headerlink" href="#subprocess.Popen.stdout" title="Permalink to this definition">¶</a></dt>
<dd><p>If the <em>stdout</em> argument was <a class="reference internal" href="#subprocess.PIPE" title="subprocess.PIPE"><tt class="xref py py-data docutils literal"><span class="pre">PIPE</span></tt></a>, this attribute is a <a class="reference internal" href="../glossary.html#term-file-object"><em class="xref std std-term">file
object</em></a> that provides output from the child process.  Otherwise, it is <tt class="xref docutils literal"><span class="pre">None</span></tt>.</p>
</dd></dl>

<dl class="attribute">
<dt id="subprocess.Popen.stderr">
<tt class="descclassname">Popen.</tt><tt class="descname">stderr</tt><a class="headerlink" href="#subprocess.Popen.stderr" title="Permalink to this definition">¶</a></dt>
<dd><p>If the <em>stderr</em> argument was <a class="reference internal" href="#subprocess.PIPE" title="subprocess.PIPE"><tt class="xref py py-data docutils literal"><span class="pre">PIPE</span></tt></a>, this attribute is a <a class="reference internal" href="../glossary.html#term-file-object"><em class="xref std std-term">file
object</em></a> that provides error output from the child process.  Otherwise, it is
<tt class="xref docutils literal"><span class="pre">None</span></tt>.</p>
</dd></dl>

<dl class="attribute">
<dt id="subprocess.Popen.pid">
<tt class="descclassname">Popen.</tt><tt class="descname">pid</tt><a class="headerlink" href="#subprocess.Popen.pid" title="Permalink to this definition">¶</a></dt>
<dd><p>The process ID of the child process.</p>
<p>Note that if you set the <em>shell</em> argument to <tt class="xref docutils literal"><span class="pre">True</span></tt>, this is the process ID
of the spawned shell.</p>
</dd></dl>

<dl class="attribute">
<dt id="subprocess.Popen.returncode">
<tt class="descclassname">Popen.</tt><tt class="descname">returncode</tt><a class="headerlink" href="#subprocess.Popen.returncode" title="Permalink to this definition">¶</a></dt>
<dd><p>The child return code, set by <a class="reference internal" href="#subprocess.Popen.poll" title="subprocess.Popen.poll"><tt class="xref py py-meth docutils literal"><span class="pre">poll()</span></tt></a> and <a class="reference internal" href="#subprocess.Popen.wait" title="subprocess.Popen.wait"><tt class="xref py py-meth docutils literal"><span class="pre">wait()</span></tt></a> (and indirectly
by <a class="reference internal" href="#subprocess.Popen.communicate" title="subprocess.Popen.communicate"><tt class="xref py py-meth docutils literal"><span class="pre">communicate()</span></tt></a>).  A <tt class="xref docutils literal"><span class="pre">None</span></tt> value indicates that the process
hasn&#8217;t terminated yet.</p>
<p>A negative value <tt class="docutils literal"><span class="pre">-N</span></tt> indicates that the child was terminated by signal
<tt class="docutils literal"><span class="pre">N</span></tt> (Unix only).</p>
</dd></dl>

</div>
<div class="section" id="windows-popen-helpers">
<h2>17.1.3. Windows Popen Helpers<a class="headerlink" href="#windows-popen-helpers" title="Permalink to this headline">¶</a></h2>
<p>The <a class="reference internal" href="#subprocess.STARTUPINFO" title="subprocess.STARTUPINFO"><tt class="xref py py-class docutils literal"><span class="pre">STARTUPINFO</span></tt></a> class and following constants are only available
on Windows.</p>
<dl class="class">
<dt id="subprocess.STARTUPINFO">
<em class="property">class </em><tt class="descclassname">subprocess.</tt><tt class="descname">STARTUPINFO</tt><a class="headerlink" href="#subprocess.STARTUPINFO" title="Permalink to this definition">¶</a></dt>
<dd><p>Partial support of the Windows
<a class="reference external" href="http://msdn.microsoft.com/en-us/library/ms686331(v=vs.85).aspx">STARTUPINFO</a>
structure is used for <a class="reference internal" href="#subprocess.Popen" title="subprocess.Popen"><tt class="xref py py-class docutils literal"><span class="pre">Popen</span></tt></a> creation.</p>
<dl class="attribute">
<dt id="subprocess.STARTUPINFO.dwFlags">
<tt class="descname">dwFlags</tt><a class="headerlink" href="#subprocess.STARTUPINFO.dwFlags" title="Permalink to this definition">¶</a></dt>
<dd><p>A bit field that determines whether certain <a class="reference internal" href="#subprocess.STARTUPINFO" title="subprocess.STARTUPINFO"><tt class="xref py py-class docutils literal"><span class="pre">STARTUPINFO</span></tt></a>
attributes are used when the process creates a window.</p>
<div class="highlight-python3"><div class="highlight"><pre><span class="n">si</span> <span class="o">=</span> <span class="n">subprocess</span><span class="o">.</span><span class="n">STARTUPINFO</span><span class="p">()</span>
<span class="n">si</span><span class="o">.</span><span class="n">dwFlags</span> <span class="o">=</span> <span class="n">subprocess</span><span class="o">.</span><span class="n">STARTF_USESTDHANDLES</span> <span class="o">|</span> <span class="n">subprocess</span><span class="o">.</span><span class="n">STARTF_USESHOWWINDOW</span>
</pre></div>
</div>
</dd></dl>

<dl class="attribute">
<dt id="subprocess.STARTUPINFO.hStdInput">
<tt class="descname">hStdInput</tt><a class="headerlink" href="#subprocess.STARTUPINFO.hStdInput" title="Permalink to this definition">¶</a></dt>
<dd><p>If <a class="reference internal" href="#subprocess.STARTUPINFO.dwFlags" title="subprocess.STARTUPINFO.dwFlags"><tt class="xref py py-attr docutils literal"><span class="pre">dwFlags</span></tt></a> specifies <a class="reference internal" href="#subprocess.STARTF_USESTDHANDLES" title="subprocess.STARTF_USESTDHANDLES"><tt class="xref py py-data docutils literal"><span class="pre">STARTF_USESTDHANDLES</span></tt></a>, this attribute
is the standard input handle for the process. If
<a class="reference internal" href="#subprocess.STARTF_USESTDHANDLES" title="subprocess.STARTF_USESTDHANDLES"><tt class="xref py py-data docutils literal"><span class="pre">STARTF_USESTDHANDLES</span></tt></a> is not specified, the default for standard
input is the keyboard buffer.</p>
</dd></dl>

<dl class="attribute">
<dt id="subprocess.STARTUPINFO.hStdOutput">
<tt class="descname">hStdOutput</tt><a class="headerlink" href="#subprocess.STARTUPINFO.hStdOutput" title="Permalink to this definition">¶</a></dt>
<dd><p>If <a class="reference internal" href="#subprocess.STARTUPINFO.dwFlags" title="subprocess.STARTUPINFO.dwFlags"><tt class="xref py py-attr docutils literal"><span class="pre">dwFlags</span></tt></a> specifies <a class="reference internal" href="#subprocess.STARTF_USESTDHANDLES" title="subprocess.STARTF_USESTDHANDLES"><tt class="xref py py-data docutils literal"><span class="pre">STARTF_USESTDHANDLES</span></tt></a>, this attribute
is the standard output handle for the process. Otherwise, this attribute
is ignored and the default for standard output is the console window&#8217;s
buffer.</p>
</dd></dl>

<dl class="attribute">
<dt id="subprocess.STARTUPINFO.hStdError">
<tt class="descname">hStdError</tt><a class="headerlink" href="#subprocess.STARTUPINFO.hStdError" title="Permalink to this definition">¶</a></dt>
<dd><p>If <a class="reference internal" href="#subprocess.STARTUPINFO.dwFlags" title="subprocess.STARTUPINFO.dwFlags"><tt class="xref py py-attr docutils literal"><span class="pre">dwFlags</span></tt></a> specifies <a class="reference internal" href="#subprocess.STARTF_USESTDHANDLES" title="subprocess.STARTF_USESTDHANDLES"><tt class="xref py py-data docutils literal"><span class="pre">STARTF_USESTDHANDLES</span></tt></a>, this attribute
is the standard error handle for the process. Otherwise, this attribute is
ignored and the default for standard error is the console window&#8217;s buffer.</p>
</dd></dl>

<dl class="attribute">
<dt id="subprocess.STARTUPINFO.wShowWindow">
<tt class="descname">wShowWindow</tt><a class="headerlink" href="#subprocess.STARTUPINFO.wShowWindow" title="Permalink to this definition">¶</a></dt>
<dd><p>If <a class="reference internal" href="#subprocess.STARTUPINFO.dwFlags" title="subprocess.STARTUPINFO.dwFlags"><tt class="xref py py-attr docutils literal"><span class="pre">dwFlags</span></tt></a> specifies <a class="reference internal" href="#subprocess.STARTF_USESHOWWINDOW" title="subprocess.STARTF_USESHOWWINDOW"><tt class="xref py py-data docutils literal"><span class="pre">STARTF_USESHOWWINDOW</span></tt></a>, this attribute
can be any of the values that can be specified in the <tt class="docutils literal"><span class="pre">nCmdShow</span></tt>
parameter for the
<a class="reference external" href="http://msdn.microsoft.com/en-us/library/ms633548(v=vs.85).aspx">ShowWindow</a>
function, except for <tt class="docutils literal"><span class="pre">SW_SHOWDEFAULT</span></tt>. Otherwise, this attribute is
ignored.</p>
<p><a class="reference internal" href="#subprocess.SW_HIDE" title="subprocess.SW_HIDE"><tt class="xref py py-data docutils literal"><span class="pre">SW_HIDE</span></tt></a> is provided for this attribute. It is used when
<a class="reference internal" href="#subprocess.Popen" title="subprocess.Popen"><tt class="xref py py-class docutils literal"><span class="pre">Popen</span></tt></a> is called with <tt class="docutils literal"><span class="pre">shell=True</span></tt>.</p>
</dd></dl>

</dd></dl>

<div class="section" id="constants">
<h3>17.1.3.1. Constants<a class="headerlink" href="#constants" title="Permalink to this headline">¶</a></h3>
<p>The <a class="reference internal" href="#module-subprocess" title="subprocess: Subprocess management."><tt class="xref py py-mod docutils literal"><span class="pre">subprocess</span></tt></a> module exposes the following constants.</p>
<dl class="data">
<dt id="subprocess.STD_INPUT_HANDLE">
<tt class="descclassname">subprocess.</tt><tt class="descname">STD_INPUT_HANDLE</tt><a class="headerlink" href="#subprocess.STD_INPUT_HANDLE" title="Permalink to this definition">¶</a></dt>
<dd><p>The standard input device. Initially, this is the console input buffer,
<tt class="docutils literal"><span class="pre">CONIN$</span></tt>.</p>
</dd></dl>

<dl class="data">
<dt id="subprocess.STD_OUTPUT_HANDLE">
<tt class="descclassname">subprocess.</tt><tt class="descname">STD_OUTPUT_HANDLE</tt><a class="headerlink" href="#subprocess.STD_OUTPUT_HANDLE" title="Permalink to this definition">¶</a></dt>
<dd><p>The standard output device. Initially, this is the active console screen
buffer, <tt class="docutils literal"><span class="pre">CONOUT$</span></tt>.</p>
</dd></dl>

<dl class="data">
<dt id="subprocess.STD_ERROR_HANDLE">
<tt class="descclassname">subprocess.</tt><tt class="descname">STD_ERROR_HANDLE</tt><a class="headerlink" href="#subprocess.STD_ERROR_HANDLE" title="Permalink to this definition">¶</a></dt>
<dd><p>The standard error device. Initially, this is the active console screen
buffer, <tt class="docutils literal"><span class="pre">CONOUT$</span></tt>.</p>
</dd></dl>

<dl class="data">
<dt id="subprocess.SW_HIDE">
<tt class="descclassname">subprocess.</tt><tt class="descname">SW_HIDE</tt><a class="headerlink" href="#subprocess.SW_HIDE" title="Permalink to this definition">¶</a></dt>
<dd><p>Hides the window. Another window will be activated.</p>
</dd></dl>

<dl class="data">
<dt id="subprocess.STARTF_USESTDHANDLES">
<tt class="descclassname">subprocess.</tt><tt class="descname">STARTF_USESTDHANDLES</tt><a class="headerlink" href="#subprocess.STARTF_USESTDHANDLES" title="Permalink to this definition">¶</a></dt>
<dd><p>Specifies that the <a class="reference internal" href="#subprocess.STARTUPINFO.hStdInput" title="subprocess.STARTUPINFO.hStdInput"><tt class="xref py py-attr docutils literal"><span class="pre">STARTUPINFO.hStdInput</span></tt></a>,
<a class="reference internal" href="#subprocess.STARTUPINFO.hStdOutput" title="subprocess.STARTUPINFO.hStdOutput"><tt class="xref py py-attr docutils literal"><span class="pre">STARTUPINFO.hStdOutput</span></tt></a>, and <a class="reference internal" href="#subprocess.STARTUPINFO.hStdError" title="subprocess.STARTUPINFO.hStdError"><tt class="xref py py-attr docutils literal"><span class="pre">STARTUPINFO.hStdError</span></tt></a> attributes
contain additional information.</p>
</dd></dl>

<dl class="data">
<dt id="subprocess.STARTF_USESHOWWINDOW">
<tt class="descclassname">subprocess.</tt><tt class="descname">STARTF_USESHOWWINDOW</tt><a class="headerlink" href="#subprocess.STARTF_USESHOWWINDOW" title="Permalink to this definition">¶</a></dt>
<dd><p>Specifies that the <a class="reference internal" href="#subprocess.STARTUPINFO.wShowWindow" title="subprocess.STARTUPINFO.wShowWindow"><tt class="xref py py-attr docutils literal"><span class="pre">STARTUPINFO.wShowWindow</span></tt></a> attribute contains
additional information.</p>
</dd></dl>

<dl class="data">
<dt id="subprocess.CREATE_NEW_CONSOLE">
<tt class="descclassname">subprocess.</tt><tt class="descname">CREATE_NEW_CONSOLE</tt><a class="headerlink" href="#subprocess.CREATE_NEW_CONSOLE" title="Permalink to this definition">¶</a></dt>
<dd><p>The new process has a new console, instead of inheriting its parent&#8217;s
console (the default).</p>
<p>This flag is always set when <a class="reference internal" href="#subprocess.Popen" title="subprocess.Popen"><tt class="xref py py-class docutils literal"><span class="pre">Popen</span></tt></a> is created with <tt class="docutils literal"><span class="pre">shell=True</span></tt>.</p>
</dd></dl>

<dl class="data">
<dt id="subprocess.CREATE_NEW_PROCESS_GROUP">
<tt class="descclassname">subprocess.</tt><tt class="descname">CREATE_NEW_PROCESS_GROUP</tt><a class="headerlink" href="#subprocess.CREATE_NEW_PROCESS_GROUP" title="Permalink to this definition">¶</a></dt>
<dd><p>A <a class="reference internal" href="#subprocess.Popen" title="subprocess.Popen"><tt class="xref py py-class docutils literal"><span class="pre">Popen</span></tt></a> <tt class="docutils literal"><span class="pre">creationflags</span></tt> parameter to specify that a new process
group will be created. This flag is necessary for using <a class="reference internal" href="os.html#os.kill" title="os.kill"><tt class="xref py py-func docutils literal"><span class="pre">os.kill()</span></tt></a>
on the subprocess.</p>
<p>This flag is ignored if <a class="reference internal" href="#subprocess.CREATE_NEW_CONSOLE" title="subprocess.CREATE_NEW_CONSOLE"><tt class="xref py py-data docutils literal"><span class="pre">CREATE_NEW_CONSOLE</span></tt></a> is specified.</p>
</dd></dl>

</div>
</div>
<div class="section" id="replacing-older-functions-with-the-subprocess-module">
<span id="subprocess-replacements"></span><h2>17.1.4. Replacing Older Functions with the subprocess Module<a class="headerlink" href="#replacing-older-functions-with-the-subprocess-module" title="Permalink to this headline">¶</a></h2>
<p>In this section, &#8220;a ==&gt; b&#8221; means that b can be used as a replacement for a.</p>
<div class="admonition note">
<p class="first admonition-title">Note</p>
<p class="last">All functions in this section fail (more or less) silently if the executed
program cannot be found; this module raises an <a class="reference internal" href="exceptions.html#OSError" title="OSError"><tt class="xref py py-exc docutils literal"><span class="pre">OSError</span></tt></a> exception.</p>
</div>
<p>In the following examples, we assume that the subprocess module is imported with
&#8220;from subprocess import *&#8221;.</p>
<div class="section" id="replacing-bin-sh-shell-backquote">
<h3>17.1.4.1. Replacing /bin/sh shell backquote<a class="headerlink" href="#replacing-bin-sh-shell-backquote" title="Permalink to this headline">¶</a></h3>
<div class="highlight-python3"><pre>output=`mycmd myarg`
==&gt;
output = Popen(["mycmd", "myarg"], stdout=PIPE).communicate()[0]</pre>
</div>
</div>
<div class="section" id="replacing-shell-pipeline">
<h3>17.1.4.2. Replacing shell pipeline<a class="headerlink" href="#replacing-shell-pipeline" title="Permalink to this headline">¶</a></h3>
<div class="highlight-python3"><pre>output=`dmesg | grep hda`
==&gt;
p1 = Popen(["dmesg"], stdout=PIPE)
p2 = Popen(["grep", "hda"], stdin=p1.stdout, stdout=PIPE)
p1.stdout.close()  # Allow p1 to receive a SIGPIPE if p2 exits.
output = p2.communicate()[0]</pre>
</div>
<p>The p1.stdout.close() call after starting the p2 is important in order for p1
to receive a SIGPIPE if p2 exits before p1.</p>
</div>
<div class="section" id="replacing-os-system">
<h3>17.1.4.3. Replacing <a class="reference internal" href="os.html#os.system" title="os.system"><tt class="xref py py-func docutils literal"><span class="pre">os.system()</span></tt></a><a class="headerlink" href="#replacing-os-system" title="Permalink to this headline">¶</a></h3>
<div class="highlight-python3"><div class="highlight"><pre><span class="n">sts</span> <span class="o">=</span> <span class="n">os</span><span class="o">.</span><span class="n">system</span><span class="p">(</span><span class="s">&quot;mycmd&quot;</span> <span class="o">+</span> <span class="s">&quot; myarg&quot;</span><span class="p">)</span>
<span class="o">==&gt;</span>
<span class="n">p</span> <span class="o">=</span> <span class="n">Popen</span><span class="p">(</span><span class="s">&quot;mycmd&quot;</span> <span class="o">+</span> <span class="s">&quot; myarg&quot;</span><span class="p">,</span> <span class="n">shell</span><span class="o">=</span><span class="k">True</span><span class="p">)</span>
<span class="n">sts</span> <span class="o">=</span> <span class="n">os</span><span class="o">.</span><span class="n">waitpid</span><span class="p">(</span><span class="n">p</span><span class="o">.</span><span class="n">pid</span><span class="p">,</span> <span class="mi">0</span><span class="p">)[</span><span class="mi">1</span><span class="p">]</span>
</pre></div>
</div>
<p>Notes:</p>
<ul class="simple">
<li>Calling the program through the shell is usually not required.</li>
<li>It&#8217;s easier to look at the <tt class="xref py py-attr docutils literal"><span class="pre">returncode</span></tt> attribute than the exit status.</li>
</ul>
<p>A more realistic example would look like this:</p>
<div class="highlight-python3"><div class="highlight"><pre><span class="k">try</span><span class="p">:</span>
    <span class="n">retcode</span> <span class="o">=</span> <span class="n">call</span><span class="p">(</span><span class="s">&quot;mycmd&quot;</span> <span class="o">+</span> <span class="s">&quot; myarg&quot;</span><span class="p">,</span> <span class="n">shell</span><span class="o">=</span><span class="k">True</span><span class="p">)</span>
    <span class="k">if</span> <span class="n">retcode</span> <span class="o">&lt;</span> <span class="mi">0</span><span class="p">:</span>
        <span class="nb">print</span><span class="p">(</span><span class="s">&quot;Child was terminated by signal&quot;</span><span class="p">,</span> <span class="o">-</span><span class="n">retcode</span><span class="p">,</span> <span class="n">file</span><span class="o">=</span><span class="n">sys</span><span class="o">.</span><span class="n">stderr</span><span class="p">)</span>
    <span class="k">else</span><span class="p">:</span>
        <span class="nb">print</span><span class="p">(</span><span class="s">&quot;Child returned&quot;</span><span class="p">,</span> <span class="n">retcode</span><span class="p">,</span> <span class="n">file</span><span class="o">=</span><span class="n">sys</span><span class="o">.</span><span class="n">stderr</span><span class="p">)</span>
<span class="k">except</span> <span class="ne">OSError</span> <span class="k">as</span> <span class="n">e</span><span class="p">:</span>
    <span class="nb">print</span><span class="p">(</span><span class="s">&quot;Execution failed:&quot;</span><span class="p">,</span> <span class="n">e</span><span class="p">,</span> <span class="n">file</span><span class="o">=</span><span class="n">sys</span><span class="o">.</span><span class="n">stderr</span><span class="p">)</span>
</pre></div>
</div>
</div>
<div class="section" id="replacing-the-os-spawn-family">
<h3>17.1.4.4. Replacing the <a class="reference internal" href="os.html#os.spawnl" title="os.spawnl"><tt class="xref py py-func docutils literal"><span class="pre">os.spawn</span></tt></a> family<a class="headerlink" href="#replacing-the-os-spawn-family" title="Permalink to this headline">¶</a></h3>
<p>P_NOWAIT example:</p>
<div class="highlight-python3"><div class="highlight"><pre><span class="n">pid</span> <span class="o">=</span> <span class="n">os</span><span class="o">.</span><span class="n">spawnlp</span><span class="p">(</span><span class="n">os</span><span class="o">.</span><span class="n">P_NOWAIT</span><span class="p">,</span> <span class="s">&quot;/bin/mycmd&quot;</span><span class="p">,</span> <span class="s">&quot;mycmd&quot;</span><span class="p">,</span> <span class="s">&quot;myarg&quot;</span><span class="p">)</span>
<span class="o">==&gt;</span>
<span class="n">pid</span> <span class="o">=</span> <span class="n">Popen</span><span class="p">([</span><span class="s">&quot;/bin/mycmd&quot;</span><span class="p">,</span> <span class="s">&quot;myarg&quot;</span><span class="p">])</span><span class="o">.</span><span class="n">pid</span>
</pre></div>
</div>
<p>P_WAIT example:</p>
<div class="highlight-python3"><div class="highlight"><pre><span class="n">retcode</span> <span class="o">=</span> <span class="n">os</span><span class="o">.</span><span class="n">spawnlp</span><span class="p">(</span><span class="n">os</span><span class="o">.</span><span class="n">P_WAIT</span><span class="p">,</span> <span class="s">&quot;/bin/mycmd&quot;</span><span class="p">,</span> <span class="s">&quot;mycmd&quot;</span><span class="p">,</span> <span class="s">&quot;myarg&quot;</span><span class="p">)</span>
<span class="o">==&gt;</span>
<span class="n">retcode</span> <span class="o">=</span> <span class="n">call</span><span class="p">([</span><span class="s">&quot;/bin/mycmd&quot;</span><span class="p">,</span> <span class="s">&quot;myarg&quot;</span><span class="p">])</span>
</pre></div>
</div>
<p>Vector example:</p>
<div class="highlight-python3"><div class="highlight"><pre><span class="n">os</span><span class="o">.</span><span class="n">spawnvp</span><span class="p">(</span><span class="n">os</span><span class="o">.</span><span class="n">P_NOWAIT</span><span class="p">,</span> <span class="n">path</span><span class="p">,</span> <span class="n">args</span><span class="p">)</span>
<span class="o">==&gt;</span>
<span class="n">Popen</span><span class="p">([</span><span class="n">path</span><span class="p">]</span> <span class="o">+</span> <span class="n">args</span><span class="p">[</span><span class="mi">1</span><span class="p">:])</span>
</pre></div>
</div>
<p>Environment example:</p>
<div class="highlight-python3"><div class="highlight"><pre><span class="n">os</span><span class="o">.</span><span class="n">spawnlpe</span><span class="p">(</span><span class="n">os</span><span class="o">.</span><span class="n">P_NOWAIT</span><span class="p">,</span> <span class="s">&quot;/bin/mycmd&quot;</span><span class="p">,</span> <span class="s">&quot;mycmd&quot;</span><span class="p">,</span> <span class="s">&quot;myarg&quot;</span><span class="p">,</span> <span class="n">env</span><span class="p">)</span>
<span class="o">==&gt;</span>
<span class="n">Popen</span><span class="p">([</span><span class="s">&quot;/bin/mycmd&quot;</span><span class="p">,</span> <span class="s">&quot;myarg&quot;</span><span class="p">],</span> <span class="n">env</span><span class="o">=</span><span class="p">{</span><span class="s">&quot;PATH&quot;</span><span class="p">:</span> <span class="s">&quot;/usr/bin&quot;</span><span class="p">})</span>
</pre></div>
</div>
</div>
<div class="section" id="replacing-os-popen-os-popen2-os-popen3">
<h3>17.1.4.5. Replacing <tt class="xref py py-func docutils literal"><span class="pre">os.popen()</span></tt>, <tt class="xref py py-func docutils literal"><span class="pre">os.popen2()</span></tt>, <tt class="xref py py-func docutils literal"><span class="pre">os.popen3()</span></tt><a class="headerlink" href="#replacing-os-popen-os-popen2-os-popen3" title="Permalink to this headline">¶</a></h3>
<div class="highlight-python3"><div class="highlight"><pre><span class="p">(</span><span class="n">child_stdin</span><span class="p">,</span> <span class="n">child_stdout</span><span class="p">)</span> <span class="o">=</span> <span class="n">os</span><span class="o">.</span><span class="n">popen2</span><span class="p">(</span><span class="n">cmd</span><span class="p">,</span> <span class="n">mode</span><span class="p">,</span> <span class="n">bufsize</span><span class="p">)</span>
<span class="o">==&gt;</span>
<span class="n">p</span> <span class="o">=</span> <span class="n">Popen</span><span class="p">(</span><span class="n">cmd</span><span class="p">,</span> <span class="n">shell</span><span class="o">=</span><span class="k">True</span><span class="p">,</span> <span class="n">bufsize</span><span class="o">=</span><span class="n">bufsize</span><span class="p">,</span>
          <span class="n">stdin</span><span class="o">=</span><span class="n">PIPE</span><span class="p">,</span> <span class="n">stdout</span><span class="o">=</span><span class="n">PIPE</span><span class="p">,</span> <span class="n">close_fds</span><span class="o">=</span><span class="k">True</span><span class="p">)</span>
<span class="p">(</span><span class="n">child_stdin</span><span class="p">,</span> <span class="n">child_stdout</span><span class="p">)</span> <span class="o">=</span> <span class="p">(</span><span class="n">p</span><span class="o">.</span><span class="n">stdin</span><span class="p">,</span> <span class="n">p</span><span class="o">.</span><span class="n">stdout</span><span class="p">)</span>
</pre></div>
</div>
<div class="highlight-python3"><div class="highlight"><pre><span class="p">(</span><span class="n">child_stdin</span><span class="p">,</span>
 <span class="n">child_stdout</span><span class="p">,</span>
 <span class="n">child_stderr</span><span class="p">)</span> <span class="o">=</span> <span class="n">os</span><span class="o">.</span><span class="n">popen3</span><span class="p">(</span><span class="n">cmd</span><span class="p">,</span> <span class="n">mode</span><span class="p">,</span> <span class="n">bufsize</span><span class="p">)</span>
<span class="o">==&gt;</span>
<span class="n">p</span> <span class="o">=</span> <span class="n">Popen</span><span class="p">(</span><span class="n">cmd</span><span class="p">,</span> <span class="n">shell</span><span class="o">=</span><span class="k">True</span><span class="p">,</span> <span class="n">bufsize</span><span class="o">=</span><span class="n">bufsize</span><span class="p">,</span>
          <span class="n">stdin</span><span class="o">=</span><span class="n">PIPE</span><span class="p">,</span> <span class="n">stdout</span><span class="o">=</span><span class="n">PIPE</span><span class="p">,</span> <span class="n">stderr</span><span class="o">=</span><span class="n">PIPE</span><span class="p">,</span> <span class="n">close_fds</span><span class="o">=</span><span class="k">True</span><span class="p">)</span>
<span class="p">(</span><span class="n">child_stdin</span><span class="p">,</span>
 <span class="n">child_stdout</span><span class="p">,</span>
 <span class="n">child_stderr</span><span class="p">)</span> <span class="o">=</span> <span class="p">(</span><span class="n">p</span><span class="o">.</span><span class="n">stdin</span><span class="p">,</span> <span class="n">p</span><span class="o">.</span><span class="n">stdout</span><span class="p">,</span> <span class="n">p</span><span class="o">.</span><span class="n">stderr</span><span class="p">)</span>
</pre></div>
</div>
<div class="highlight-python3"><div class="highlight"><pre><span class="p">(</span><span class="n">child_stdin</span><span class="p">,</span> <span class="n">child_stdout_and_stderr</span><span class="p">)</span> <span class="o">=</span> <span class="n">os</span><span class="o">.</span><span class="n">popen4</span><span class="p">(</span><span class="n">cmd</span><span class="p">,</span> <span class="n">mode</span><span class="p">,</span> <span class="n">bufsize</span><span class="p">)</span>
<span class="o">==&gt;</span>
<span class="n">p</span> <span class="o">=</span> <span class="n">Popen</span><span class="p">(</span><span class="n">cmd</span><span class="p">,</span> <span class="n">shell</span><span class="o">=</span><span class="k">True</span><span class="p">,</span> <span class="n">bufsize</span><span class="o">=</span><span class="n">bufsize</span><span class="p">,</span>
          <span class="n">stdin</span><span class="o">=</span><span class="n">PIPE</span><span class="p">,</span> <span class="n">stdout</span><span class="o">=</span><span class="n">PIPE</span><span class="p">,</span> <span class="n">stderr</span><span class="o">=</span><span class="n">STDOUT</span><span class="p">,</span> <span class="n">close_fds</span><span class="o">=</span><span class="k">True</span><span class="p">)</span>
<span class="p">(</span><span class="n">child_stdin</span><span class="p">,</span> <span class="n">child_stdout_and_stderr</span><span class="p">)</span> <span class="o">=</span> <span class="p">(</span><span class="n">p</span><span class="o">.</span><span class="n">stdin</span><span class="p">,</span> <span class="n">p</span><span class="o">.</span><span class="n">stdout</span><span class="p">)</span>
</pre></div>
</div>
<p>Return code handling translates as follows:</p>
<div class="highlight-python3"><div class="highlight"><pre><span class="n">pipe</span> <span class="o">=</span> <span class="n">os</span><span class="o">.</span><span class="n">popen</span><span class="p">(</span><span class="n">cmd</span><span class="p">,</span> <span class="s">&#39;w&#39;</span><span class="p">)</span>
<span class="o">...</span>
<span class="n">rc</span> <span class="o">=</span> <span class="n">pipe</span><span class="o">.</span><span class="n">close</span><span class="p">()</span>
<span class="k">if</span> <span class="n">rc</span> <span class="ow">is</span> <span class="ow">not</span> <span class="k">None</span> <span class="ow">and</span> <span class="n">rc</span> <span class="o">&gt;&gt;</span> <span class="mi">8</span><span class="p">:</span>
    <span class="nb">print</span><span class="p">(</span><span class="s">&quot;There were some errors&quot;</span><span class="p">)</span>
<span class="o">==&gt;</span>
<span class="n">process</span> <span class="o">=</span> <span class="n">Popen</span><span class="p">(</span><span class="n">cmd</span><span class="p">,</span> <span class="s">&#39;w&#39;</span><span class="p">,</span> <span class="n">stdin</span><span class="o">=</span><span class="n">PIPE</span><span class="p">)</span>
<span class="o">...</span>
<span class="n">process</span><span class="o">.</span><span class="n">stdin</span><span class="o">.</span><span class="n">close</span><span class="p">()</span>
<span class="k">if</span> <span class="n">process</span><span class="o">.</span><span class="n">wait</span><span class="p">()</span> <span class="o">!=</span> <span class="mi">0</span><span class="p">:</span>
    <span class="nb">print</span><span class="p">(</span><span class="s">&quot;There were some errors&quot;</span><span class="p">)</span>
</pre></div>
</div>
</div>
<div class="section" id="replacing-functions-from-the-popen2-module">
<h3>17.1.4.6. Replacing functions from the <tt class="xref py py-mod docutils literal"><span class="pre">popen2</span></tt> module<a class="headerlink" href="#replacing-functions-from-the-popen2-module" title="Permalink to this headline">¶</a></h3>
<div class="admonition note">
<p class="first admonition-title">Note</p>
<p class="last">If the cmd argument to popen2 functions is a string, the command is executed
through /bin/sh.  If it is a list, the command is directly executed.</p>
</div>
<div class="highlight-python3"><div class="highlight"><pre><span class="p">(</span><span class="n">child_stdout</span><span class="p">,</span> <span class="n">child_stdin</span><span class="p">)</span> <span class="o">=</span> <span class="n">popen2</span><span class="o">.</span><span class="n">popen2</span><span class="p">(</span><span class="s">&quot;somestring&quot;</span><span class="p">,</span> <span class="n">bufsize</span><span class="p">,</span> <span class="n">mode</span><span class="p">)</span>
<span class="o">==&gt;</span>
<span class="n">p</span> <span class="o">=</span> <span class="n">Popen</span><span class="p">([</span><span class="s">&quot;somestring&quot;</span><span class="p">],</span> <span class="n">shell</span><span class="o">=</span><span class="k">True</span><span class="p">,</span> <span class="n">bufsize</span><span class="o">=</span><span class="n">bufsize</span><span class="p">,</span>
          <span class="n">stdin</span><span class="o">=</span><span class="n">PIPE</span><span class="p">,</span> <span class="n">stdout</span><span class="o">=</span><span class="n">PIPE</span><span class="p">,</span> <span class="n">close_fds</span><span class="o">=</span><span class="k">True</span><span class="p">)</span>
<span class="p">(</span><span class="n">child_stdout</span><span class="p">,</span> <span class="n">child_stdin</span><span class="p">)</span> <span class="o">=</span> <span class="p">(</span><span class="n">p</span><span class="o">.</span><span class="n">stdout</span><span class="p">,</span> <span class="n">p</span><span class="o">.</span><span class="n">stdin</span><span class="p">)</span>
</pre></div>
</div>
<div class="highlight-python3"><div class="highlight"><pre><span class="p">(</span><span class="n">child_stdout</span><span class="p">,</span> <span class="n">child_stdin</span><span class="p">)</span> <span class="o">=</span> <span class="n">popen2</span><span class="o">.</span><span class="n">popen2</span><span class="p">([</span><span class="s">&quot;mycmd&quot;</span><span class="p">,</span> <span class="s">&quot;myarg&quot;</span><span class="p">],</span> <span class="n">bufsize</span><span class="p">,</span> <span class="n">mode</span><span class="p">)</span>
<span class="o">==&gt;</span>
<span class="n">p</span> <span class="o">=</span> <span class="n">Popen</span><span class="p">([</span><span class="s">&quot;mycmd&quot;</span><span class="p">,</span> <span class="s">&quot;myarg&quot;</span><span class="p">],</span> <span class="n">bufsize</span><span class="o">=</span><span class="n">bufsize</span><span class="p">,</span>
          <span class="n">stdin</span><span class="o">=</span><span class="n">PIPE</span><span class="p">,</span> <span class="n">stdout</span><span class="o">=</span><span class="n">PIPE</span><span class="p">,</span> <span class="n">close_fds</span><span class="o">=</span><span class="k">True</span><span class="p">)</span>
<span class="p">(</span><span class="n">child_stdout</span><span class="p">,</span> <span class="n">child_stdin</span><span class="p">)</span> <span class="o">=</span> <span class="p">(</span><span class="n">p</span><span class="o">.</span><span class="n">stdout</span><span class="p">,</span> <span class="n">p</span><span class="o">.</span><span class="n">stdin</span><span class="p">)</span>
</pre></div>
</div>
<p><tt class="xref py py-class docutils literal"><span class="pre">popen2.Popen3</span></tt> and <tt class="xref py py-class docutils literal"><span class="pre">popen2.Popen4</span></tt> basically work as
<a class="reference internal" href="#subprocess.Popen" title="subprocess.Popen"><tt class="xref py py-class docutils literal"><span class="pre">subprocess.Popen</span></tt></a>, except that:</p>
<ul class="simple">
<li><a class="reference internal" href="#subprocess.Popen" title="subprocess.Popen"><tt class="xref py py-class docutils literal"><span class="pre">Popen</span></tt></a> raises an exception if the execution fails.</li>
<li>the <em>capturestderr</em> argument is replaced with the <em>stderr</em> argument.</li>
<li><tt class="docutils literal"><span class="pre">stdin=PIPE</span></tt> and <tt class="docutils literal"><span class="pre">stdout=PIPE</span></tt> must be specified.</li>
<li>popen2 closes all file descriptors by default, but you have to specify
<tt class="docutils literal"><span class="pre">close_fds=True</span></tt> with <a class="reference internal" href="#subprocess.Popen" title="subprocess.Popen"><tt class="xref py py-class docutils literal"><span class="pre">Popen</span></tt></a> to guarantee this behavior on
all platforms or past Python versions.</li>
</ul>
</div>
</div>
<div class="section" id="notes">
<h2>17.1.5. Notes<a class="headerlink" href="#notes" title="Permalink to this headline">¶</a></h2>
<div class="section" id="converting-an-argument-sequence-to-a-string-on-windows">
<span id="converting-argument-sequence"></span><h3>17.1.5.1. Converting an argument sequence to a string on Windows<a class="headerlink" href="#converting-an-argument-sequence-to-a-string-on-windows" title="Permalink to this headline">¶</a></h3>
<p>On Windows, an <em>args</em> sequence is converted to a string that can be parsed
using the following rules (which correspond to the rules used by the MS C
runtime):</p>
<ol class="arabic simple">
<li>Arguments are delimited by white space, which is either a
space or a tab.</li>
<li>A string surrounded by double quotation marks is
interpreted as a single argument, regardless of white space
contained within.  A quoted string can be embedded in an
argument.</li>
<li>A double quotation mark preceded by a backslash is
interpreted as a literal double quotation mark.</li>
<li>Backslashes are interpreted literally, unless they
immediately precede a double quotation mark.</li>
<li>If backslashes immediately precede a double quotation mark,
every pair of backslashes is interpreted as a literal
backslash.  If the number of backslashes is odd, the last
backslash escapes the next double quotation mark as
described in rule 3.</li>
</ol>
</div>
</div>
</div>


          </div>
        </div>
      </div>
      <div class="sphinxsidebar">
        <div class="sphinxsidebarwrapper">
  <h3><a href="../contents.html">Table Of Contents</a></h3>
  <ul>
<li><a class="reference internal" href="#">17.1. <tt class="docutils literal"><span class="pre">subprocess</span></tt> &#8212; Subprocess management</a><ul>
<li><a class="reference internal" href="#using-the-subprocess-module">17.1.1. Using the subprocess Module</a><ul>
<li><a class="reference internal" href="#convenience-functions">17.1.1.1. Convenience Functions</a></li>
<li><a class="reference internal" href="#exceptions">17.1.1.2. Exceptions</a></li>
<li><a class="reference internal" href="#security">17.1.1.3. Security</a></li>
</ul>
</li>
<li><a class="reference internal" href="#popen-objects">17.1.2. Popen Objects</a></li>
<li><a class="reference internal" href="#windows-popen-helpers">17.1.3. Windows Popen Helpers</a><ul>
<li><a class="reference internal" href="#constants">17.1.3.1. Constants</a></li>
</ul>
</li>
<li><a class="reference internal" href="#replacing-older-functions-with-the-subprocess-module">17.1.4. Replacing Older Functions with the subprocess Module</a><ul>
<li><a class="reference internal" href="#replacing-bin-sh-shell-backquote">17.1.4.1. Replacing /bin/sh shell backquote</a></li>
<li><a class="reference internal" href="#replacing-shell-pipeline">17.1.4.2. Replacing shell pipeline</a></li>
<li><a class="reference internal" href="#replacing-os-system">17.1.4.3. Replacing <tt class="docutils literal"><span class="pre">os.system()</span></tt></a></li>
<li><a class="reference internal" href="#replacing-the-os-spawn-family">17.1.4.4. Replacing the <tt class="docutils literal"><span class="pre">os.spawn</span></tt> family</a></li>
<li><a class="reference internal" href="#replacing-os-popen-os-popen2-os-popen3">17.1.4.5. Replacing <tt class="docutils literal"><span class="pre">os.popen()</span></tt>, <tt class="docutils literal"><span class="pre">os.popen2()</span></tt>, <tt class="docutils literal"><span class="pre">os.popen3()</span></tt></a></li>
<li><a class="reference internal" href="#replacing-functions-from-the-popen2-module">17.1.4.6. Replacing functions from the <tt class="docutils literal"><span class="pre">popen2</span></tt> module</a></li>
</ul>
</li>
<li><a class="reference internal" href="#notes">17.1.5. Notes</a><ul>
<li><a class="reference internal" href="#converting-an-argument-sequence-to-a-string-on-windows">17.1.5.1. Converting an argument sequence to a string on Windows</a></li>
</ul>
</li>
</ul>
</li>
</ul>

  <h4>Previous topic</h4>
  <p class="topless"><a href="ipc.html"
                        title="previous chapter">17. Interprocess Communication and Networking</a></p>
  <h4>Next topic</h4>
  <p class="topless"><a href="socket.html"
                        title="next chapter">17.2. <tt class="docutils literal docutils literal docutils literal"><span class="pre">socket</span></tt> &#8212; Low-level networking interface</a></p>
<h3>This Page</h3>
<ul class="this-page-menu">
  <li><a href="../bugs.html">Report a Bug</a></li>
  <li><a href="../_sources/library/subprocess.txt"
         rel="nofollow">Show Source</a></li>
</ul>

<div id="searchbox" style="display: none">
  <h3>Quick search</h3>
    <form class="search" action="../search.html" method="get">
      <input type="text" name="q" size="18" />
      <input type="submit" value="Go" />
      <input type="hidden" name="check_keywords" value="yes" />
      <input type="hidden" name="area" value="default" />
    </form>
    <p class="searchtip" style="font-size: 90%">
    Enter search terms or a module, class or function name.
    </p>
</div>
<script type="text/javascript">$('#searchbox').show(0);</script>
        </div>
      </div>
      <div class="clearer"></div>
    </div>
    <div class="related">
      <h3>Navigation</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="../genindex.html" title="General Index"
             >index</a></li>
        <li class="right" >
          <a href="../py-modindex.html" title="Python Module Index"
             >modules</a> |</li>
        <li class="right" >
          <a href="socket.html" title="17.2. socket — Low-level networking interface"
             >next</a> |</li>
        <li class="right" >
          <a href="ipc.html" title="17. Interprocess Communication and Networking"
             >previous</a> |</li>
        <li><img src="../_static/py.png" alt=""
                 style="vertical-align: middle; margin-top: -1px"/></li>
        <li><a href="../index.html">Python v3.2.2 documentation</a> &raquo;</li>

          <li><a href="index.html" >The Python Standard Library</a> &raquo;</li>
          <li><a href="ipc.html" >17. Interprocess Communication and Networking</a> &raquo;</li> 
      </ul>
    </div>
    <div class="footer">
    &copy; <a href="../copyright.html">Copyright</a> 1990-2011, Python Software Foundation.
    <br />
    The Python Software Foundation is a non-profit corporation.  
    <a href="http://www.python.org/psf/donations/">Please donate.</a>
    <br />
    Last updated on Sep 04, 2011.
    <a href="../bugs.html">Found a bug</a>?
    <br />
    Created using <a href="http://sphinx.pocoo.org/">Sphinx</a> 1.0.7.
    </div>

  </body>
</html>