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="os-fd-ops.html" /> | |
13 | <link rel="prev" href="os-procinfo.html" /> | |
14 | <link rel="parent" href="module-os.html" /> | |
15 | <link rel="next" href="os-fd-ops.html" /> | |
16 | <meta name='aesop' content='information' /> | |
17 | <title>6.1.2 File Object Creation </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.1.1 Process Parameters" | |
25 | href="os-procinfo.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.1 os " | |
28 | href="module-os.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.1.3 File Descriptor Operations" | |
31 | href="os-fd-ops.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="os-procinfo.html">6.1.1 Process Parameters</A> | |
46 | <b class="navlabel">Up:</b> | |
47 | <a class="sectref" rel="parent" href="module-os.html">6.1 os </A> | |
48 | <b class="navlabel">Next:</b> | |
49 | <a class="sectref" rel="next" href="os-fd-ops.html">6.1.3 File Descriptor Operations</A> | |
50 | </div> | |
51 | <hr /></div> | |
52 | </DIV> | |
53 | <!--End of Navigation Panel--> | |
54 | ||
55 | <H2><A NAME="SECTION008120000000000000000"></A><A NAME="os-newstreams"></A> | |
56 | <BR> | |
57 | 6.1.2 File Object Creation | |
58 | </H2> | |
59 | ||
60 | <P> | |
61 | These functions create new file objects. | |
62 | ||
63 | <P> | |
64 | <dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"> | |
65 | <td><nobr><b><tt id='l2h-1551' xml:id='l2h-1551' class="function">fdopen</tt></b>(</nobr></td> | |
66 | <td><var>fd</var><big>[</big><var>, mode</var><big>[</big><var>, bufsize</var><big>]</big><var></var><big>]</big><var></var>)</td></tr></table></dt> | |
67 | <dd> | |
68 | Return an open file object connected to the file descriptor <var>fd</var>. | |
69 | <a id='l2h-1557' xml:id='l2h-1557'></a> | |
70 | The <var>mode</var> and <var>bufsize</var> arguments have the same meaning as | |
71 | the corresponding arguments to the built-in <tt class="function">open()</tt> | |
72 | function. | |
73 | Availability: Macintosh, <span class="Unix">Unix</span>, Windows. | |
74 | ||
75 | <P> | |
76 | ||
77 | <span class="versionnote">Changed in version 2.3: | |
78 | When specified, the <var>mode</var> argument must now start | |
79 | with one of the letters "<tt class="character">r</tt>", "<tt class="character">w</tt>", or "<tt class="character">a</tt>", | |
80 | otherwise a <tt class="exception">ValueError</tt> is raised.</span> | |
81 | ||
82 | </dl> | |
83 | ||
84 | <P> | |
85 | <dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"> | |
86 | <td><nobr><b><tt id='l2h-1552' xml:id='l2h-1552' class="function">popen</tt></b>(</nobr></td> | |
87 | <td><var>command</var><big>[</big><var>, mode</var><big>[</big><var>, bufsize</var><big>]</big><var></var><big>]</big><var></var>)</td></tr></table></dt> | |
88 | <dd> | |
89 | Open a pipe to or from <var>command</var>. The return value is an open | |
90 | file object connected to the pipe, which can be read or written | |
91 | depending on whether <var>mode</var> is <code>'r'</code> (default) or <code>'w'</code>. | |
92 | The <var>bufsize</var> argument has the same meaning as the corresponding | |
93 | argument to the built-in <tt class="function">open()</tt> function. The exit status of | |
94 | the command (encoded in the format specified for <tt class="function">wait()</tt>) is | |
95 | available as the return value of the <tt class="method">close()</tt> method of the file | |
96 | object, except that when the exit status is zero (termination without | |
97 | errors), <code>None</code> is returned. | |
98 | Availability: Macintosh, <span class="Unix">Unix</span>, Windows. | |
99 | ||
100 | <P> | |
101 | ||
102 | <span class="versionnote">Changed in version 2.0: | |
103 | This function worked unreliably under Windows in | |
104 | earlier versions of Python. This was due to the use of the | |
105 | <tt class="cfunction">_popen()</tt> function from the libraries provided with | |
106 | Windows. Newer versions of Python do not use the broken | |
107 | implementation from the Windows libraries.</span> | |
108 | ||
109 | </dl> | |
110 | ||
111 | <P> | |
112 | <dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"> | |
113 | <td><nobr><b><tt id='l2h-1553' xml:id='l2h-1553' class="function">tmpfile</tt></b>(</nobr></td> | |
114 | <td><var></var>)</td></tr></table></dt> | |
115 | <dd> | |
116 | Return a new file object opened in update mode ("<tt class="samp">w+b</tt>"). The file | |
117 | has no directory entries associated with it and will be automatically | |
118 | deleted once there are no file descriptors for the file. | |
119 | Availability: Macintosh, <span class="Unix">Unix</span>, Windows. | |
120 | </dl> | |
121 | ||
122 | <P> | |
123 | For each of the following <tt class="function">popen()</tt> variants, if <var>bufsize</var> is | |
124 | specified, it specifies the buffer size for the I/O pipes. | |
125 | <var>mode</var>, if provided, should be the string <code>'b'</code> or | |
126 | <code>'t'</code>; on Windows this is needed to determine whether the file | |
127 | objects should be opened in binary or text mode. The default value | |
128 | for <var>mode</var> is <code>'t'</code>. | |
129 | ||
130 | <P> | |
131 | Also, for each of these variants, on <span class="Unix">Unix</span>, <var>cmd</var> may be a sequence, in | |
132 | which case arguments will be passed directly to the program without shell | |
133 | intervention (as with <tt class="function">os.spawnv()</tt>). If <var>cmd</var> is a string it will | |
134 | be passed to the shell (as with <tt class="function">os.system()</tt>). | |
135 | ||
136 | <P> | |
137 | These methods do not make it possible to retrieve the exit status from | |
138 | the child processes. The only way to control the input and output | |
139 | streams and also retrieve the return codes is to use the | |
140 | <tt class="class">Popen3</tt> and <tt class="class">Popen4</tt> classes from the <tt class="module"><a href="module-popen2.html">popen2</a></tt> | |
141 | module; these are only available on <span class="Unix">Unix</span>. | |
142 | ||
143 | <P> | |
144 | For a discussion of possible deadlock conditions related to the use | |
145 | of these functions, see ``<a class="ulink" href="popen2-flow-control.html" | |
146 | >Flow Control | |
147 | Issues</a>'' | |
148 | (section <A href="popen2-flow-control.html#popen2-flow-control">6.9.2</A>). | |
149 | ||
150 | <P> | |
151 | <dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"> | |
152 | <td><nobr><b><tt id='l2h-1554' xml:id='l2h-1554' class="function">popen2</tt></b>(</nobr></td> | |
153 | <td><var>cmd</var><big>[</big><var>, mode</var><big>[</big><var>, bufsize</var><big>]</big><var></var><big>]</big><var></var>)</td></tr></table></dt> | |
154 | <dd> | |
155 | Executes <var>cmd</var> as a sub-process. Returns the file objects | |
156 | <code>(<var>child_stdin</var>, <var>child_stdout</var>)</code>. | |
157 | Availability: Macintosh, <span class="Unix">Unix</span>, Windows. | |
158 | ||
159 | <span class="versionnote">New in version 2.0.</span> | |
160 | ||
161 | </dl> | |
162 | ||
163 | <P> | |
164 | <dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"> | |
165 | <td><nobr><b><tt id='l2h-1555' xml:id='l2h-1555' class="function">popen3</tt></b>(</nobr></td> | |
166 | <td><var>cmd</var><big>[</big><var>, mode</var><big>[</big><var>, bufsize</var><big>]</big><var></var><big>]</big><var></var>)</td></tr></table></dt> | |
167 | <dd> | |
168 | Executes <var>cmd</var> as a sub-process. Returns the file objects | |
169 | <code>(<var>child_stdin</var>, <var>child_stdout</var>, <var>child_stderr</var>)</code>. | |
170 | Availability: Macintosh, <span class="Unix">Unix</span>, Windows. | |
171 | ||
172 | <span class="versionnote">New in version 2.0.</span> | |
173 | ||
174 | </dl> | |
175 | ||
176 | <P> | |
177 | <dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"> | |
178 | <td><nobr><b><tt id='l2h-1556' xml:id='l2h-1556' class="function">popen4</tt></b>(</nobr></td> | |
179 | <td><var>cmd</var><big>[</big><var>, mode</var><big>[</big><var>, bufsize</var><big>]</big><var></var><big>]</big><var></var>)</td></tr></table></dt> | |
180 | <dd> | |
181 | Executes <var>cmd</var> as a sub-process. Returns the file objects | |
182 | <code>(<var>child_stdin</var>, <var>child_stdout_and_stderr</var>)</code>. | |
183 | Availability: Macintosh, <span class="Unix">Unix</span>, Windows. | |
184 | ||
185 | <span class="versionnote">New in version 2.0.</span> | |
186 | ||
187 | </dl> | |
188 | ||
189 | <P> | |
190 | (Note that <code><var>child_stdin</var>, <var>child_stdout</var>, and | |
191 | <var>child_stderr</var></code> are named from the point of view of the child | |
192 | process, i.e. <var>child_stdin</var> is the child's standard input.) | |
193 | ||
194 | <P> | |
195 | This functionality is also available in the <tt class="module"><a href="module-popen2.html">popen2</a></tt> module | |
196 | using functions of the same names, but the return values of those | |
197 | functions have a different order. | |
198 | ||
199 | <P> | |
200 | ||
201 | <DIV CLASS="navigation"> | |
202 | <div class='online-navigation'> | |
203 | <p></p><hr /> | |
204 | <table align="center" width="100%" cellpadding="0" cellspacing="2"> | |
205 | <tr> | |
206 | <td class='online-navigation'><a rel="prev" title="6.1.1 Process Parameters" | |
207 | href="os-procinfo.html"><img src='../icons/previous.png' | |
208 | border='0' height='32' alt='Previous Page' width='32' /></A></td> | |
209 | <td class='online-navigation'><a rel="parent" title="6.1 os " | |
210 | href="module-os.html"><img src='../icons/up.png' | |
211 | border='0' height='32' alt='Up One Level' width='32' /></A></td> | |
212 | <td class='online-navigation'><a rel="next" title="6.1.3 File Descriptor Operations" | |
213 | href="os-fd-ops.html"><img src='../icons/next.png' | |
214 | border='0' height='32' alt='Next Page' width='32' /></A></td> | |
215 | <td align="center" width="100%">Python Library Reference</td> | |
216 | <td class='online-navigation'><a rel="contents" title="Table of Contents" | |
217 | href="contents.html"><img src='../icons/contents.png' | |
218 | border='0' height='32' alt='Contents' width='32' /></A></td> | |
219 | <td class='online-navigation'><a href="modindex.html" title="Module Index"><img src='../icons/modules.png' | |
220 | border='0' height='32' alt='Module Index' width='32' /></a></td> | |
221 | <td class='online-navigation'><a rel="index" title="Index" | |
222 | href="genindex.html"><img src='../icons/index.png' | |
223 | border='0' height='32' alt='Index' width='32' /></A></td> | |
224 | </tr></table> | |
225 | <div class='online-navigation'> | |
226 | <b class="navlabel">Previous:</b> | |
227 | <a class="sectref" rel="prev" href="os-procinfo.html">6.1.1 Process Parameters</A> | |
228 | <b class="navlabel">Up:</b> | |
229 | <a class="sectref" rel="parent" href="module-os.html">6.1 os </A> | |
230 | <b class="navlabel">Next:</b> | |
231 | <a class="sectref" rel="next" href="os-fd-ops.html">6.1.3 File Descriptor Operations</A> | |
232 | </div> | |
233 | </div> | |
234 | <hr /> | |
235 | <span class="release-info">Release 2.4.2, documentation updated on 28 September 2005.</span> | |
236 | </DIV> | |
237 | <!--End of Navigation Panel--> | |
238 | <ADDRESS> | |
239 | See <i><a href="about.html">About this document...</a></i> for information on suggesting changes. | |
240 | </ADDRESS> | |
241 | </BODY> | |
242 | </HTML> |