Commit | Line | Data |
---|---|---|
920dae64 AT |
1 | <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> |
2 | <html> | |
3 | <head> | |
4 | <link rel="STYLESHEET" href="lib.css" type='text/css' /> | |
5 | <link rel="SHORTCUT ICON" href="../icons/pyfav.png" type="image/png" /> | |
6 | <link rel='start' href='../index.html' title='Python Documentation Index' /> | |
7 | <link rel="first" href="lib.html" title='Python Library Reference' /> | |
8 | <link rel='contents' href='contents.html' title="Contents" /> | |
9 | <link rel='index' href='genindex.html' title='Index' /> | |
10 | <link rel='last' href='about.html' title='About this document...' /> | |
11 | <link rel='help' href='about.html' title='About this document...' /> | |
12 | <link rel="next" href="node239.html" /> | |
13 | <link rel="prev" href="module-subprocess.html" /> | |
14 | <link rel="parent" href="module-subprocess.html" /> | |
15 | <link rel="next" href="node236.html" /> | |
16 | <meta name='aesop' content='information' /> | |
17 | <title>6.8.1 Using the subprocess Module</title> | |
18 | </head> | |
19 | <body> | |
20 | <DIV CLASS="navigation"> | |
21 | <div id='top-navigation-panel' xml:id='top-navigation-panel'> | |
22 | <table align="center" width="100%" cellpadding="0" cellspacing="2"> | |
23 | <tr> | |
24 | <td class='online-navigation'><a rel="prev" title="6.8 subprocess " | |
25 | href="module-subprocess.html"><img src='../icons/previous.png' | |
26 | border='0' height='32' alt='Previous Page' width='32' /></A></td> | |
27 | <td class='online-navigation'><a rel="parent" title="6.8 subprocess " | |
28 | href="module-subprocess.html"><img src='../icons/up.png' | |
29 | border='0' height='32' alt='Up One Level' width='32' /></A></td> | |
30 | <td class='online-navigation'><a rel="next" title="6.8.1.1 Convenience Functions" | |
31 | href="node236.html"><img src='../icons/next.png' | |
32 | border='0' height='32' alt='Next Page' width='32' /></A></td> | |
33 | <td align="center" width="100%">Python Library Reference</td> | |
34 | <td class='online-navigation'><a rel="contents" title="Table of Contents" | |
35 | href="contents.html"><img src='../icons/contents.png' | |
36 | border='0' height='32' alt='Contents' width='32' /></A></td> | |
37 | <td class='online-navigation'><a href="modindex.html" title="Module Index"><img src='../icons/modules.png' | |
38 | border='0' height='32' alt='Module Index' width='32' /></a></td> | |
39 | <td class='online-navigation'><a rel="index" title="Index" | |
40 | href="genindex.html"><img src='../icons/index.png' | |
41 | border='0' height='32' alt='Index' width='32' /></A></td> | |
42 | </tr></table> | |
43 | <div class='online-navigation'> | |
44 | <b class="navlabel">Previous:</b> | |
45 | <a class="sectref" rel="prev" href="module-subprocess.html">6.8 subprocess </A> | |
46 | <b class="navlabel">Up:</b> | |
47 | <a class="sectref" rel="parent" href="module-subprocess.html">6.8 subprocess </A> | |
48 | <b class="navlabel">Next:</b> | |
49 | <a class="sectref" rel="next" href="node236.html">6.8.1.1 Convenience Functions</A> | |
50 | </div> | |
51 | <hr /></div> | |
52 | </DIV> | |
53 | <!--End of Navigation Panel--> | |
54 | ||
55 | <H2><A NAME="SECTION008810000000000000000"> | |
56 | 6.8.1 Using the subprocess Module</A> | |
57 | </H2> | |
58 | ||
59 | <P> | |
60 | This module defines one class called <tt class="class">Popen</tt>: | |
61 | ||
62 | <P> | |
63 | <dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"> | |
64 | <td><nobr><b><span class="typelabel">class</span> <tt id='l2h-1833' xml:id='l2h-1833' class="class">Popen</tt></b>(</nobr></td> | |
65 | <td><var>args, bufsize=0, executable=None, | |
66 | stdin=None, stdout=None, stderr=None, | |
67 | preexec_fn=None, close_fds=False, shell=False, | |
68 | cwd=None, env=None, universal_newlines=False, | |
69 | startupinfo=None, creationflags=0</var>)</td></tr></table></dt> | |
70 | <dd> | |
71 | ||
72 | <P> | |
73 | Arguments are: | |
74 | ||
75 | <P> | |
76 | <var>args</var> should be a string, or a sequence of program arguments. The | |
77 | program to execute is normally the first item in the args sequence or | |
78 | string, but can be explicitly set by using the executable argument. | |
79 | ||
80 | <P> | |
81 | On <span class="Unix">Unix</span>, with <var>shell=False</var> (default): In this case, the Popen | |
82 | class uses <tt class="method">os.execvp()</tt> to execute the child program. | |
83 | <var>args</var> should normally be a sequence. A string will be treated as a | |
84 | sequence with the string as the only item (the program to execute). | |
85 | ||
86 | <P> | |
87 | On <span class="Unix">Unix</span>, with <var>shell=True</var>: If args is a string, it specifies the | |
88 | command string to execute through the shell. If <var>args</var> is a | |
89 | sequence, the first item specifies the command string, and any | |
90 | additional items will be treated as additional shell arguments. | |
91 | ||
92 | <P> | |
93 | On Windows: the <tt class="class">Popen</tt> class uses CreateProcess() to execute | |
94 | the child program, which operates on strings. If <var>args</var> is a | |
95 | sequence, it will be converted to a string using the | |
96 | <tt class="method">list2cmdline</tt> method. Please note that not all MS Windows | |
97 | applications interpret the command line the same way: | |
98 | <tt class="method">list2cmdline</tt> is designed for applications using the same | |
99 | rules as the MS C runtime. | |
100 | ||
101 | <P> | |
102 | <var>bufsize</var>, if given, has the same meaning as the corresponding | |
103 | argument to the built-in open() function: <tt class="constant">0</tt> means unbuffered, | |
104 | <tt class="constant">1</tt> means line buffered, any other positive value means use a | |
105 | buffer of (approximately) that size. A negative <var>bufsize</var> means to | |
106 | use the system default, which usually means fully buffered. The default | |
107 | value for <var>bufsize</var> is <tt class="constant">0</tt> (unbuffered). | |
108 | ||
109 | <P> | |
110 | The <var>executable</var> argument specifies the program to execute. It is | |
111 | very seldom needed: Usually, the program to execute is defined by the | |
112 | <var>args</var> argument. If <var>shell=True</var>, the <var>executable</var> | |
113 | argument specifies which shell to use. On <span class="Unix">Unix</span>, the default shell | |
114 | is /bin/sh. On Windows, the default shell is specified by the COMSPEC | |
115 | environment variable. | |
116 | ||
117 | <P> | |
118 | <var>stdin</var>, <var>stdout</var> and <var>stderr</var> specify the executed | |
119 | programs' standard input, standard output and standard error file | |
120 | handles, respectively. Valid values are <code>PIPE</code>, an existing file | |
121 | descriptor (a positive integer), an existing file object, and | |
122 | <code>None</code>. <code>PIPE</code> indicates that a new pipe to the child | |
123 | should be created. With <code>None</code>, no redirection will occur; the | |
124 | child's file handles will be inherited from the parent. Additionally, | |
125 | <var>stderr</var> can be <code>STDOUT</code>, which indicates that the stderr | |
126 | data from the applications should be captured into the same file | |
127 | handle as for stdout. | |
128 | ||
129 | <P> | |
130 | If <var>preexec_fn</var> is set to a callable object, this object will be | |
131 | called in the child process just before the child is executed. | |
132 | ||
133 | <P> | |
134 | If <var>close_fds</var> is true, all file descriptors except <tt class="constant">0</tt>, | |
135 | <tt class="constant">1</tt> and <tt class="constant">2</tt> will be closed before the child process is | |
136 | executed. | |
137 | ||
138 | <P> | |
139 | If <var>shell</var> is <tt class="constant">True</tt>, the specified command will be | |
140 | executed through the shell. | |
141 | ||
142 | <P> | |
143 | If <var>cwd</var> is not <code>None</code>, the current directory will be changed | |
144 | to cwd before the child is executed. | |
145 | ||
146 | <P> | |
147 | If <var>env</var> is not <code>None</code>, it defines the environment variables | |
148 | for the new process. | |
149 | ||
150 | <P> | |
151 | If <var>universal_newlines</var> is <tt class="constant">True</tt>, the file objects stdout | |
152 | and stderr are opened as a text files, but lines may be terminated by | |
153 | any of <code>'\n'</code>, the Unix end-of-line convention, <code>'\r'</code>, | |
154 | the Macintosh convention or <code>'\r\n'</code>, the Windows convention. | |
155 | All of these external representations are seen as <code>'\n'</code> by the | |
156 | Python program. <span class="note"><b class="label">Note:</b> | |
157 | This feature is only available if Python is built | |
158 | with universal newline support (the default). Also, the newlines | |
159 | attribute of the file objects <tt class="member">stdout</tt>, <tt class="member">stdin</tt> and | |
160 | <tt class="member">stderr</tt> are not updated by the communicate() method.</span> | |
161 | ||
162 | <P> | |
163 | The <var>startupinfo</var> and <var>creationflags</var>, if given, will be | |
164 | passed to the underlying CreateProcess() function. They can specify | |
165 | things such as appearance of the main window and priority for the new | |
166 | process. (Windows only) | |
167 | </dl> | |
168 | ||
169 | <P> | |
170 | ||
171 | <p><br /></p><hr class='online-navigation' /> | |
172 | <div class='online-navigation'> | |
173 | <!--Table of Child-Links--> | |
174 | <A NAME="CHILD_LINKS"><STRONG>Subsections</STRONG></a> | |
175 | ||
176 | <UL CLASS="ChildLinks"> | |
177 | <LI><A href="node236.html">6.8.1.1 Convenience Functions</a> | |
178 | <LI><A href="node237.html">6.8.1.2 Exceptions</a> | |
179 | <LI><A href="node238.html">6.8.1.3 Security</a> | |
180 | </ul> | |
181 | <!--End of Table of Child-Links--> | |
182 | </div> | |
183 | ||
184 | <DIV CLASS="navigation"> | |
185 | <div class='online-navigation'> | |
186 | <p></p><hr /> | |
187 | <table align="center" width="100%" cellpadding="0" cellspacing="2"> | |
188 | <tr> | |
189 | <td class='online-navigation'><a rel="prev" title="6.8 subprocess " | |
190 | href="module-subprocess.html"><img src='../icons/previous.png' | |
191 | border='0' height='32' alt='Previous Page' width='32' /></A></td> | |
192 | <td class='online-navigation'><a rel="parent" title="6.8 subprocess " | |
193 | href="module-subprocess.html"><img src='../icons/up.png' | |
194 | border='0' height='32' alt='Up One Level' width='32' /></A></td> | |
195 | <td class='online-navigation'><a rel="next" title="6.8.1.1 Convenience Functions" | |
196 | href="node236.html"><img src='../icons/next.png' | |
197 | border='0' height='32' alt='Next Page' width='32' /></A></td> | |
198 | <td align="center" width="100%">Python Library Reference</td> | |
199 | <td class='online-navigation'><a rel="contents" title="Table of Contents" | |
200 | href="contents.html"><img src='../icons/contents.png' | |
201 | border='0' height='32' alt='Contents' width='32' /></A></td> | |
202 | <td class='online-navigation'><a href="modindex.html" title="Module Index"><img src='../icons/modules.png' | |
203 | border='0' height='32' alt='Module Index' width='32' /></a></td> | |
204 | <td class='online-navigation'><a rel="index" title="Index" | |
205 | href="genindex.html"><img src='../icons/index.png' | |
206 | border='0' height='32' alt='Index' width='32' /></A></td> | |
207 | </tr></table> | |
208 | <div class='online-navigation'> | |
209 | <b class="navlabel">Previous:</b> | |
210 | <a class="sectref" rel="prev" href="module-subprocess.html">6.8 subprocess </A> | |
211 | <b class="navlabel">Up:</b> | |
212 | <a class="sectref" rel="parent" href="module-subprocess.html">6.8 subprocess </A> | |
213 | <b class="navlabel">Next:</b> | |
214 | <a class="sectref" rel="next" href="node236.html">6.8.1.1 Convenience Functions</A> | |
215 | </div> | |
216 | </div> | |
217 | <hr /> | |
218 | <span class="release-info">Release 2.4.2, documentation updated on 28 September 2005.</span> | |
219 | </DIV> | |
220 | <!--End of Navigation Panel--> | |
221 | <ADDRESS> | |
222 | See <i><a href="about.html">About this document...</a></i> for information on suggesting changes. | |
223 | </ADDRESS> | |
224 | </BODY> | |
225 | </HTML> |