<!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=
"node239.html" />
<link rel=
"prev" href=
"module-subprocess.html" />
<link rel=
"parent" href=
"module-subprocess.html" />
<link rel=
"next" href=
"node236.html" />
<meta name='aesop' content='information'
/>
<title>6.8.1 Using the subprocess Module
</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 subprocess "
href=
"module-subprocess.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.8 subprocess "
href=
"module-subprocess.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.8.1.1 Convenience Functions"
href=
"node236.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=
"module-subprocess.html">6.8 subprocess
</A>
<b class=
"navlabel">Up:
</b>
<a class=
"sectref" rel=
"parent" href=
"module-subprocess.html">6.8 subprocess
</A>
<b class=
"navlabel">Next:
</b>
<a class=
"sectref" rel=
"next" href=
"node236.html">6.8.1.1 Convenience Functions
</A>
<!--End of Navigation Panel-->
<H2><A NAME=
"SECTION008810000000000000000">
6.8.1 Using the subprocess Module
</A>
This module defines one class called
<tt class=
"class">Popen
</tt>:
<dl><dt><table cellpadding=
"0" cellspacing=
"0"><tr valign=
"baseline">
<td><nobr><b><span class=
"typelabel">class
</span> <tt id='l2h-
1833' xml:id='l2h-
1833'
class=
"class">Popen
</tt></b>(
</nobr></td>
<td><var>args, bufsize=
0, executable=None,
stdin=None, stdout=None, stderr=None,
preexec_fn=None, close_fds=False, shell=False,
cwd=None, env=None, universal_newlines=False,
startupinfo=None, creationflags=
0</var>)
</td></tr></table></dt>
<var>args
</var> should be a string, or a sequence of program arguments. The
program to execute is normally the first item in the args sequence or
string, but can be explicitly set by using the executable argument.
On
<span class=
"Unix">Unix
</span>, with
<var>shell=False
</var> (default): In this case, the Popen
class uses
<tt class=
"method">os.execvp()
</tt> to execute the child program.
<var>args
</var> should normally be a sequence. A string will be treated as a
sequence with the string as the only item (the program to execute).
On
<span class=
"Unix">Unix
</span>, with
<var>shell=True
</var>: If args is a string, it specifies the
command string to execute through the shell. If
<var>args
</var> is a
sequence, the first item specifies the command string, and any
additional items will be treated as additional shell arguments.
On Windows: the
<tt class=
"class">Popen
</tt> class uses CreateProcess() to execute
the child program, which operates on strings. If
<var>args
</var> is a
sequence, it will be converted to a string using the
<tt class=
"method">list2cmdline
</tt> method. Please note that not all MS Windows
applications interpret the command line the same way:
<tt class=
"method">list2cmdline
</tt> is designed for applications using the same
rules as the MS C runtime.
<var>bufsize
</var>, if given, has the same meaning as the corresponding
argument to the built-in open() function:
<tt class=
"constant">0</tt> means unbuffered,
<tt class=
"constant">1</tt> means line buffered, any other positive value means use a
buffer of (approximately) that size. A negative
<var>bufsize
</var> means to
use the system default, which usually means fully buffered. The default
value for
<var>bufsize
</var> is
<tt class=
"constant">0</tt> (unbuffered).
The
<var>executable
</var> argument specifies the program to execute. It is
very seldom needed: Usually, the program to execute is defined by the
<var>args
</var> argument. If
<var>shell=True
</var>, the
<var>executable
</var>
argument specifies which shell to use. On
<span class=
"Unix">Unix
</span>, the default shell
is /bin/sh. On Windows, the default shell is specified by the COMSPEC
<var>stdin
</var>,
<var>stdout
</var> and
<var>stderr
</var> specify the executed
programs' standard input, standard output and standard error file
handles, respectively. Valid values are
<code>PIPE
</code>, an existing file
descriptor (a positive integer), an existing file object, and
<code>None
</code>.
<code>PIPE
</code> indicates that a new pipe to the child
should be created. With
<code>None
</code>, no redirection will occur; the
child's file handles will be inherited from the parent. Additionally,
<var>stderr
</var> can be
<code>STDOUT
</code>, which indicates that the stderr
data from the applications should be captured into the same file
If
<var>preexec_fn
</var> is set to a callable object, this object will be
called in the child process just before the child is executed.
If
<var>close_fds
</var> is true, all file descriptors except
<tt class=
"constant">0</tt>,
<tt class=
"constant">1</tt> and
<tt class=
"constant">2</tt> will be closed before the child process is
If
<var>shell
</var> is
<tt class=
"constant">True
</tt>, the specified command will be
executed through the shell.
If
<var>cwd
</var> is not
<code>None
</code>, the current directory will be changed
to cwd before the child is executed.
If
<var>env
</var> is not
<code>None
</code>, it defines the environment variables
If
<var>universal_newlines
</var> is
<tt class=
"constant">True
</tt>, the file objects stdout
and stderr are opened as a text files, but lines may be terminated by
any of
<code>'
\n'
</code>, the Unix end-of-line convention,
<code>'
\r'
</code>,
the Macintosh convention or
<code>'
\r
\n'
</code>, the Windows convention.
All of these external representations are seen as
<code>'
\n'
</code> by the
Python program.
<span class=
"note"><b class=
"label">Note:
</b>
This feature is only available if Python is built
with universal newline support (the default). Also, the newlines
attribute of the file objects
<tt class=
"member">stdout
</tt>,
<tt class=
"member">stdin
</tt> and
<tt class=
"member">stderr
</tt> are not updated by the communicate() method.
</span>
The
<var>startupinfo
</var> and
<var>creationflags
</var>, if given, will be
passed to the underlying CreateProcess() function. They can specify
things such as appearance of the main window and priority for the new
<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=
"node236.html">6.8.1.1 Convenience Functions
</a>
<LI><A href=
"node237.html">6.8.1.2 Exceptions
</a>
<LI><A href=
"node238.html">6.8.1.3 Security
</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 subprocess "
href=
"module-subprocess.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.8 subprocess "
href=
"module-subprocess.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.8.1.1 Convenience Functions"
href=
"node236.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=
"module-subprocess.html">6.8 subprocess
</A>
<b class=
"navlabel">Up:
</b>
<a class=
"sectref" rel=
"parent" href=
"module-subprocess.html">6.8 subprocess
</A>
<b class=
"navlabel">Next:
</b>
<a class=
"sectref" rel=
"next" href=
"node236.html">6.8.1.1 Convenience Functions
</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.