<!DOCTYPE html PUBLIC
"-//W3C//DTD HTML 4.0 Transitional//EN">
<link rel=
"STYLESHEET" href=
"lib.css" type='text/css'
/>
<link rel=
"SHORTCUT ICON" href=
"../icons/pyfav.png" type=
"image/png" />
<link rel='start' href='../index.html' title='Python Documentation Index'
/>
<link rel=
"first" href=
"lib.html" title='Python Library Reference'
/>
<link rel='contents' href='contents.html'
title=
"Contents" />
<link rel='index' href='genindex.html' title='Index'
/>
<link rel='last' href='about.html' title='About this document...'
/>
<link rel='help' href='about.html' title='About this document...'
/>
<link rel=
"next" href=
"module-datetime.html" />
<link rel=
"prev" href=
"module-subprocess.html" />
<link rel=
"parent" href=
"allos.html" />
<link rel=
"next" href=
"popen3-objects.html" />
<meta name='aesop' content='information'
/>
<title>6.9 popen2 -- Subprocesses with accessible I/O streams
</title>
<div id='top-navigation-panel' xml:id='top-navigation-panel'
>
<table align=
"center" width=
"100%" cellpadding=
"0" cellspacing=
"2">
<td class='online-navigation'
><a rel=
"prev" title=
"6.8.3.6 Replacing popen2.*"
href=
"node246.html"><img src='../icons/previous.png'
border='
0' height='
32' alt='Previous Page' width='
32'
/></A></td>
<td class='online-navigation'
><a rel=
"parent" title=
"6. Generic Operating System"
href=
"allos.html"><img src='../icons/up.png'
border='
0' height='
32' alt='Up One Level' width='
32'
/></A></td>
<td class='online-navigation'
><a rel=
"next" title=
"6.9.1 Popen3 and Popen4"
href=
"popen3-objects.html"><img src='../icons/next.png'
border='
0' height='
32' alt='Next Page' width='
32'
/></A></td>
<td align=
"center" width=
"100%">Python Library Reference
</td>
<td class='online-navigation'
><a rel=
"contents" title=
"Table of Contents"
href=
"contents.html"><img src='../icons/contents.png'
border='
0' height='
32' alt='Contents' width='
32'
/></A></td>
<td class='online-navigation'
><a href=
"modindex.html" title=
"Module Index"><img src='../icons/modules.png'
border='
0' height='
32' alt='Module Index' width='
32'
/></a></td>
<td class='online-navigation'
><a rel=
"index" title=
"Index"
href=
"genindex.html"><img src='../icons/index.png'
border='
0' height='
32' alt='Index' width='
32'
/></A></td>
<div class='online-navigation'
>
<b class=
"navlabel">Previous:
</b>
<a class=
"sectref" rel=
"prev" href=
"node246.html">6.8.3.6 Replacing popen2.*
</A>
<b class=
"navlabel">Up:
</b>
<a class=
"sectref" rel=
"parent" href=
"allos.html">6. Generic Operating System
</A>
<b class=
"navlabel">Next:
</b>
<a class=
"sectref" rel=
"next" href=
"popen3-objects.html">6.9.1 Popen3 and Popen4
</A>
<!--End of Navigation Panel-->
<H1><A NAME=
"SECTION008900000000000000000">
6.9 <tt class=
"module">popen2
</tt> --
Subprocesses with accessible I/O streams
</A>
<A NAME=
"module-popen2"></A>
<p class=
"availability">Availability:
<span
class=
"platform">Unix, Windows
</span>.
</p>
This module allows you to spawn processes and connect to their
input/output/error pipes and obtain their return codes under
<span class=
"Unix">Unix
</span> and Windows.
Note that starting with Python
2.0, this functionality is available
using functions from the
<tt class=
"module"><a href=
"module-os.html">os
</a></tt> module which have the same
names as the factory functions here, but the order of the return
values is more intuitive in the
<tt class=
"module"><a href=
"module-os.html">os
</a></tt> module variants.
The primary interface offered by this module is a trio of factory
functions. For each of these, if
<var>bufsize
</var> is specified,
it specifies the buffer size for the I/O pipes.
<var>mode
</var>, if
provided, should be the string
<code>'b'
</code> or
<code>'t'
</code>; on Windows
this is needed to determine whether the file objects should be opened
in binary or text mode. The default value for
<var>mode
</var> is
On
<span class=
"Unix">Unix
</span>,
<var>cmd
</var> may be a sequence, in which case arguments will be passed
directly to the program without shell intervention (as with
<tt class=
"function">os.spawnv()
</tt>). If
<var>cmd
</var> is a string it will be passed to the
shell (as with
<tt class=
"function">os.system()
</tt>).
The only way to retrieve the return codes for the child processes is
by using the
<tt class=
"method">poll()
</tt> or
<tt class=
"method">wait()
</tt> methods on the
<tt class=
"class">Popen3
</tt> and
<tt class=
"class">Popen4
</tt> classes; these are only available on
<span class=
"Unix">Unix
</span>. This information is not available when using the
<tt class=
"function">popen2()
</tt>,
<tt class=
"function">popen3()
</tt>, and
<tt class=
"function">popen4()
</tt>
functions, or the equivalent functions in the
<tt class=
"module"><a href=
"module-os.html">os
</a></tt> module.
(Note that the tuples returned by the
<tt class=
"module"><a href=
"module-os.html">os
</a></tt> module's functions
are in a different order from the ones returned by the
<tt class=
"module">popen2
</tt>
<dl><dt><table cellpadding=
"0" cellspacing=
"0"><tr valign=
"baseline">
<td><nobr><b><tt id='l2h-
1844' xml:id='l2h-
1844'
class=
"function">popen2
</tt></b>(
</nobr></td>
<td><var>cmd
</var><big>[
</big><var>, bufsize
</var><big>[
</big><var>, mode
</var><big>]
</big><var></var><big>]
</big><var></var>)
</td></tr></table></dt>
Executes
<var>cmd
</var> as a sub-process. Returns the file objects
<code>(
<var>child_stdout
</var>,
<var>child_stdin
</var>)
</code>.
<dl><dt><table cellpadding=
"0" cellspacing=
"0"><tr valign=
"baseline">
<td><nobr><b><tt id='l2h-
1845' xml:id='l2h-
1845'
class=
"function">popen3
</tt></b>(
</nobr></td>
<td><var>cmd
</var><big>[
</big><var>, bufsize
</var><big>[
</big><var>, mode
</var><big>]
</big><var></var><big>]
</big><var></var>)
</td></tr></table></dt>
Executes
<var>cmd
</var> as a sub-process. Returns the file objects
<code>(
<var>child_stdout
</var>,
<var>child_stdin
</var>,
<var>child_stderr
</var>)
</code>.
<dl><dt><table cellpadding=
"0" cellspacing=
"0"><tr valign=
"baseline">
<td><nobr><b><tt id='l2h-
1846' xml:id='l2h-
1846'
class=
"function">popen4
</tt></b>(
</nobr></td>
<td><var>cmd
</var><big>[
</big><var>, bufsize
</var><big>[
</big><var>, mode
</var><big>]
</big><var></var><big>]
</big><var></var>)
</td></tr></table></dt>
Executes
<var>cmd
</var> as a sub-process. Returns the file objects
<code>(
<var>child_stdout_and_stderr
</var>,
<var>child_stdin
</var>)
</code>.
<span class=
"versionnote">New in version
2.0.
</span>
On
<span class=
"Unix">Unix
</span>, a class defining the objects returned by the factory
functions is also available. These are not used for the Windows
implementation, and are not available on that platform.
<dl><dt><table cellpadding=
"0" cellspacing=
"0"><tr valign=
"baseline">
<td><nobr><b><span class=
"typelabel">class
</span> <tt id='l2h-
1847' xml:id='l2h-
1847'
class=
"class">Popen3
</tt></b>(
</nobr></td>
<td><var>cmd
</var><big>[
</big><var>, capturestderr
</var><big>[
</big><var>, bufsize
</var><big>]
</big><var></var><big>]
</big><var></var>)
</td></tr></table></dt>
This class represents a child process. Normally,
<tt class=
"class">Popen3
</tt>
instances are created using the
<tt class=
"function">popen2()
</tt> and
<tt class=
"function">popen3()
</tt> factory functions described above.
If not using one of the helper functions to create
<tt class=
"class">Popen3
</tt>
objects, the parameter
<var>cmd
</var> is the shell command to execute in a
sub-process. The
<var>capturestderr
</var> flag, if true, specifies that
the object should capture standard error output of the child process.
The default is false. If the
<var>bufsize
</var> parameter is specified, it
specifies the size of the I/O buffers to/from the child process.
<dl><dt><table cellpadding=
"0" cellspacing=
"0"><tr valign=
"baseline">
<td><nobr><b><span class=
"typelabel">class
</span> <tt id='l2h-
1848' xml:id='l2h-
1848'
class=
"class">Popen4
</tt></b>(
</nobr></td>
<td><var>cmd
</var><big>[
</big><var>, bufsize
</var><big>]
</big><var></var>)
</td></tr></table></dt>
Similar to
<tt class=
"class">Popen3
</tt>, but always captures standard error into the
same file object as standard output. These are typically created
using
<tt class=
"function">popen4()
</tt>.
<span class=
"versionnote">New in version
2.0.
</span>
<p><br /></p><hr class='online-navigation'
/>
<div class='online-navigation'
>
<!--Table of Child-Links-->
<A NAME=
"CHILD_LINKS"><STRONG>Subsections
</STRONG></a>
<LI><A href=
"popen3-objects.html">6.9.1 Popen3 and Popen4 Objects
</a>
<LI><A href=
"popen2-flow-control.html">6.9.2 Flow Control Issues
</a>
<!--End of Table of Child-Links-->
<div class='online-navigation'
>
<table align=
"center" width=
"100%" cellpadding=
"0" cellspacing=
"2">
<td class='online-navigation'
><a rel=
"prev" title=
"6.8.3.6 Replacing popen2.*"
href=
"node246.html"><img src='../icons/previous.png'
border='
0' height='
32' alt='Previous Page' width='
32'
/></A></td>
<td class='online-navigation'
><a rel=
"parent" title=
"6. Generic Operating System"
href=
"allos.html"><img src='../icons/up.png'
border='
0' height='
32' alt='Up One Level' width='
32'
/></A></td>
<td class='online-navigation'
><a rel=
"next" title=
"6.9.1 Popen3 and Popen4"
href=
"popen3-objects.html"><img src='../icons/next.png'
border='
0' height='
32' alt='Next Page' width='
32'
/></A></td>
<td align=
"center" width=
"100%">Python Library Reference
</td>
<td class='online-navigation'
><a rel=
"contents" title=
"Table of Contents"
href=
"contents.html"><img src='../icons/contents.png'
border='
0' height='
32' alt='Contents' width='
32'
/></A></td>
<td class='online-navigation'
><a href=
"modindex.html" title=
"Module Index"><img src='../icons/modules.png'
border='
0' height='
32' alt='Module Index' width='
32'
/></a></td>
<td class='online-navigation'
><a rel=
"index" title=
"Index"
href=
"genindex.html"><img src='../icons/index.png'
border='
0' height='
32' alt='Index' width='
32'
/></A></td>
<div class='online-navigation'
>
<b class=
"navlabel">Previous:
</b>
<a class=
"sectref" rel=
"prev" href=
"node246.html">6.8.3.6 Replacing popen2.*
</A>
<b class=
"navlabel">Up:
</b>
<a class=
"sectref" rel=
"parent" href=
"allos.html">6. Generic Operating System
</A>
<b class=
"navlabel">Next:
</b>
<a class=
"sectref" rel=
"next" href=
"popen3-objects.html">6.9.1 Popen3 and Popen4
</A>
<span class=
"release-info">Release
2.4.2, documentation updated on
28 September
2005.
</span>
<!--End of Navigation Panel-->
See
<i><a href=
"about.html">About this document...
</a></i> for information on suggesting changes.