Initial commit of OpenSPARC T2 architecture model.
[OpenSPARC-T2-SAM] / sam-t2 / devtools / amd64 / html / python / lib / module-tempfile.html
CommitLineData
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="module-errno.html" />
13<link rel="prev" href="module-optparse.html" />
14<link rel="parent" href="allos.html" />
15<link rel="next" href="module-errno.html" />
16<meta name='aesop' content='information' />
17<title>6.22 tempfile -- Generate temporary files and directories</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.21.5.3 Other reasons to"
25 href="optparse-other-reasons-to-extend-optparse.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. Generic Operating System"
28 href="allos.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.23 errno "
31 href="module-errno.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="optparse-other-reasons-to-extend-optparse.html">6.21.5.3 Other reasons to</A>
46<b class="navlabel">Up:</b>
47<a class="sectref" rel="parent" href="allos.html">6. Generic Operating System</A>
48<b class="navlabel">Next:</b>
49<a class="sectref" rel="next" href="module-errno.html">6.23 errno </A>
50</div>
51<hr /></div>
52</DIV>
53<!--End of Navigation Panel-->
54
55<H1><A NAME="SECTION0082200000000000000000">
566.22 <tt class="module">tempfile</tt> --
57 Generate temporary files and directories</A>
58</H1>
59
60<P>
61<A NAME="module-tempfile"></A>
62
63<P>
64<a id='l2h-2199' xml:id='l2h-2199'></a><a id='l2h-2200' xml:id='l2h-2200'></a>
65<P>
66This module generates temporary files and directories. It works on
67all supported platforms.
68
69<P>
70In version 2.3 of Python, this module was overhauled for enhanced
71security. It now provides three new functions,
72<tt class="function">NamedTemporaryFile()</tt>, <tt class="function">mkstemp()</tt>, and
73<tt class="function">mkdtemp()</tt>, which should eliminate all remaining need to use
74the insecure <tt class="function">mktemp()</tt> function. Temporary file names created
75by this module no longer contain the process ID; instead a string of
76six random characters is used.
77
78<P>
79Also, all the user-callable functions now take additional arguments
80which allow direct control over the location and name of temporary
81files. It is no longer necessary to use the global <var>tempdir</var> and
82<var>template</var> variables. To maintain backward compatibility, the
83argument order is somewhat odd; it is recommended to use keyword
84arguments for clarity.
85
86<P>
87The module defines the following user-callable functions:
88
89<P>
90<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
91 <td><nobr><b><tt id='l2h-2201' xml:id='l2h-2201' class="function">TemporaryFile</tt></b>(</nobr></td>
92 <td><var></var><big>[</big><var>mode=<code>'w+b'</code></var><big>[</big><var>,
93 bufsize=<code>-1</code></var><big>[</big><var>,
94 suffix</var><big>[</big><var>, prefix</var><big>[</big><var>, dir</var><big>]</big><var></var><big>]</big><var></var><big>]</big><var></var><big>]</big><var></var><big>]</big><var></var>)</td></tr></table></dt>
95<dd>
96Return a file (or file-like) object that can be used as a temporary
97storage area. The file is created using <tt class="function">mkstemp</tt>. It will
98be destroyed as soon as it is closed (including an implicit close when
99the object is garbage collected). Under <span class="Unix">Unix</span>, the directory entry
100for the file is removed immediately after the file is created. Other
101platforms do not support this; your code should not rely on a
102temporary file created using this function having or not having a
103visible name in the file system.
104
105<P>
106The <var>mode</var> parameter defaults to <code>'w+b'</code> so that the file
107created can be read and written without being closed. Binary mode is
108used so that it behaves consistently on all platforms without regard
109for the data that is stored. <var>bufsize</var> defaults to <code>-1</code>,
110meaning that the operating system default is used.
111
112<P>
113The <var>dir</var>, <var>prefix</var> and <var>suffix</var> parameters are passed to
114<tt class="function">mkstemp()</tt>.
115</dl>
116
117<P>
118<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
119 <td><nobr><b><tt id='l2h-2202' xml:id='l2h-2202' class="function">NamedTemporaryFile</tt></b>(</nobr></td>
120 <td><var></var><big>[</big><var>mode=<code>'w+b'</code></var><big>[</big><var>,
121 bufsize=<code>-1</code></var><big>[</big><var>,
122 suffix</var><big>[</big><var>, prefix</var><big>[</big><var>,
123 dir</var><big>]</big><var></var><big>]</big><var></var><big>]</big><var></var><big>]</big><var></var><big>]</big><var></var>)</td></tr></table></dt>
124<dd>
125This function operates exactly as <tt class="function">TemporaryFile()</tt> does,
126except that the file is guaranteed to have a visible name in the file
127system (on <span class="Unix">Unix</span>, the directory entry is not unlinked). That name can
128be retrieved from the <tt class="member">name</tt> member of the file object. Whether
129the name can be used to open the file a second time, while the
130named temporary file is still open, varies across platforms (it can
131be so used on <span class="Unix">Unix</span>; it cannot on Windows NT or later).
132
133<span class="versionnote">New in version 2.3.</span>
134
135</dl>
136
137<P>
138<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
139 <td><nobr><b><tt id='l2h-2203' xml:id='l2h-2203' class="function">mkstemp</tt></b>(</nobr></td>
140 <td><var></var><big>[</big><var>suffix</var><big>[</big><var>,
141 prefix</var><big>[</big><var>, dir</var><big>[</big><var>, text</var><big>]</big><var></var><big>]</big><var></var><big>]</big><var></var><big>]</big><var></var>)</td></tr></table></dt>
142<dd>
143Creates a temporary file in the most secure manner possible. There
144are no race conditions in the file's creation, assuming that the
145platform properly implements the <tt class="constant">O_EXCL</tt> flag for
146<tt class="function">os.open()</tt>. The file is readable and writable only by the
147creating user ID. If the platform uses permission bits to indicate
148whether a file is executable, the file is executable by no one. The
149file descriptor is not inherited by child processes.
150
151<P>
152Unlike <tt class="function">TemporaryFile()</tt>, the user of <tt class="function">mkstemp()</tt> is
153responsible for deleting the temporary file when done with it.
154
155<P>
156If <var>suffix</var> is specified, the file name will end with that suffix,
157otherwise there will be no suffix. <tt class="function">mkstemp()</tt> does not put a
158dot between the file name and the suffix; if you need one, put it at
159the beginning of <var>suffix</var>.
160
161<P>
162If <var>prefix</var> is specified, the file name will begin with that
163prefix; otherwise, a default prefix is used.
164
165<P>
166If <var>dir</var> is specified, the file will be created in that directory;
167otherwise, a default directory is used.
168
169<P>
170If <var>text</var> is specified, it indicates whether to open the file in
171binary mode (the default) or text mode. On some platforms, this makes
172no difference.
173
174<P>
175<tt class="function">mkstemp()</tt> returns a tuple containing an OS-level handle to
176an open file (as would be returned by <tt class="function">os.open()</tt>) and the
177absolute pathname of that file, in that order.
178
179<span class="versionnote">New in version 2.3.</span>
180
181</dl>
182
183<P>
184<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
185 <td><nobr><b><tt id='l2h-2204' xml:id='l2h-2204' class="function">mkdtemp</tt></b>(</nobr></td>
186 <td><var></var><big>[</big><var>suffix</var><big>[</big><var>, prefix</var><big>[</big><var>, dir</var><big>]</big><var></var><big>]</big><var></var><big>]</big><var></var>)</td></tr></table></dt>
187<dd>
188Creates a temporary directory in the most secure manner possible.
189There are no race conditions in the directory's creation. The
190directory is readable, writable, and searchable only by the
191creating user ID.
192
193<P>
194The user of <tt class="function">mkdtemp()</tt> is responsible for deleting the
195temporary directory and its contents when done with it.
196
197<P>
198The <var>prefix</var>, <var>suffix</var>, and <var>dir</var> arguments are the same
199as for <tt class="function">mkstemp()</tt>.
200
201<P>
202<tt class="function">mkdtemp()</tt> returns the absolute pathname of the new directory.
203
204<span class="versionnote">New in version 2.3.</span>
205
206</dl>
207
208<P>
209<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
210 <td><nobr><b><tt id='l2h-2205' xml:id='l2h-2205' class="function">mktemp</tt></b>(</nobr></td>
211 <td><var></var><big>[</big><var>suffix</var><big>[</big><var>, prefix</var><big>[</big><var>, dir</var><big>]</big><var></var><big>]</big><var></var><big>]</big><var></var>)</td></tr></table></dt>
212<dd>
213<div class="versionnote"><b>Deprecated since release 2.3.</b>
214Use <tt class="function">mkstemp()</tt> instead.</div><p></p>
215Return an absolute pathname of a file that did not exist at the time
216the call is made. The <var>prefix</var>, <var>suffix</var>, and <var>dir</var>
217arguments are the same as for <tt class="function">mkstemp()</tt>.
218
219<P>
220<span class="warning"><b class="label">Warning:</b>
221Use of this function may introduce a security hole in your
222program. By the time you get around to doing anything with the file
223name it returns, someone else may have beaten you to the punch.</span>
224</dl>
225
226<P>
227The module uses two global variables that tell it how to construct a
228temporary name. They are initialized at the first call to any of the
229functions above. The caller may change them, but this is discouraged;
230use the appropriate function arguments, instead.
231
232<P>
233<dl><dt><b><tt id='l2h-2206' xml:id='l2h-2206'>tempdir</tt></b></dt>
234<dd>
235When set to a value other than <code>None</code>, this variable defines the
236default value for the <var>dir</var> argument to all the functions defined
237in this module.
238
239<P>
240If <code>tempdir</code> is unset or <code>None</code> at any call to any of the
241above functions, Python searches a standard list of directories and
242sets <var>tempdir</var> to the first one which the calling user can create
243files in. The list is:
244
245<P>
246
247<OL>
248<LI>The directory named by the <a class="envvar" id='l2h-2210' xml:id='l2h-2210'>TMPDIR</a> environment variable.
249</LI>
250<LI>The directory named by the <a class="envvar" id='l2h-2211' xml:id='l2h-2211'>TEMP</a> environment variable.
251</LI>
252<LI>The directory named by the <a class="envvar" id='l2h-2212' xml:id='l2h-2212'>TMP</a> environment variable.
253</LI>
254<LI>A platform-specific location:
255
256<UL>
257<LI>On RiscOS, the directory named by the
258 <a class="envvar" id='l2h-2213' xml:id='l2h-2213'>Wimp$ScrapDir</a> environment variable.
259</LI>
260<LI>On Windows, the directories
261 <span class="file">C:<SPAN CLASS="MATH"><IMG
262 WIDTH="12" HEIGHT="32" ALIGN="MIDDLE" BORDER="0"
263 SRC="img2.png"
264 ALT="$\backslash$"></SPAN>TEMP</span>,
265 <span class="file">C:<SPAN CLASS="MATH"><IMG
266 WIDTH="12" HEIGHT="32" ALIGN="MIDDLE" BORDER="0"
267 SRC="img2.png"
268 ALT="$\backslash$"></SPAN>TMP</span>,
269 <span class="file"><SPAN CLASS="MATH"><IMG
270 WIDTH="12" HEIGHT="32" ALIGN="MIDDLE" BORDER="0"
271 SRC="img2.png"
272 ALT="$\backslash$"></SPAN>TEMP</span>, and
273 <span class="file"><SPAN CLASS="MATH"><IMG
274 WIDTH="12" HEIGHT="32" ALIGN="MIDDLE" BORDER="0"
275 SRC="img2.png"
276 ALT="$\backslash$"></SPAN>TMP</span>, in that order.
277</LI>
278<LI>On all other platforms, the directories
279 <span class="file">/tmp</span>, <span class="file">/var/tmp</span>, and <span class="file">/usr/tmp</span>, in that order.
280
281</LI>
282</UL>
283</LI>
284<LI>As a last resort, the current working directory.
285</LI>
286</OL>
287</dd></dl>
288
289<P>
290<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
291 <td><nobr><b><tt id='l2h-2207' xml:id='l2h-2207' class="function">gettempdir</tt></b>(</nobr></td>
292 <td><var></var>)</td></tr></table></dt>
293<dd>
294Return the directory currently selected to create temporary files in.
295If <code>tempdir</code> is not <code>None</code>, this simply returns its contents;
296otherwise, the search described above is performed, and the result
297returned.
298</dl>
299
300<P>
301<dl><dt><b><tt id='l2h-2208' xml:id='l2h-2208'>template</tt></b></dt>
302<dd>
303<div class="versionnote"><b>Deprecated since release 2.0.</b>
304Use <tt class="function">gettempprefix()</tt> instead.</div><p></p>
305When set to a value other than <code>None</code>, this variable defines the
306prefix of the final component of the filenames returned by
307<tt class="function">mktemp()</tt>. A string of six random letters and digits is
308appended to the prefix to make the filename unique. On Windows,
309the default prefix is <span class="file">~T</span>; on all other systems
310it is <span class="file">tmp</span>.
311
312<P>
313Older versions of this module used to require that <code>template</code> be
314set to <code>None</code> after a call to <tt class="function">os.fork()</tt>; this has not
315been necessary since version 1.5.2.
316</dd></dl>
317
318<P>
319<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
320 <td><nobr><b><tt id='l2h-2209' xml:id='l2h-2209' class="function">gettempprefix</tt></b>(</nobr></td>
321 <td><var></var>)</td></tr></table></dt>
322<dd>
323Return the filename prefix used to create temporary files. This does
324not contain the directory component. Using this function is preferred
325over reading the <var>template</var> variable directly.
326
327<span class="versionnote">New in version 1.5.2.</span>
328
329</dl>
330
331<DIV CLASS="navigation">
332<div class='online-navigation'>
333<p></p><hr />
334<table align="center" width="100%" cellpadding="0" cellspacing="2">
335<tr>
336<td class='online-navigation'><a rel="prev" title="6.21.5.3 Other reasons to"
337 href="optparse-other-reasons-to-extend-optparse.html"><img src='../icons/previous.png'
338 border='0' height='32' alt='Previous Page' width='32' /></A></td>
339<td class='online-navigation'><a rel="parent" title="6. Generic Operating System"
340 href="allos.html"><img src='../icons/up.png'
341 border='0' height='32' alt='Up One Level' width='32' /></A></td>
342<td class='online-navigation'><a rel="next" title="6.23 errno "
343 href="module-errno.html"><img src='../icons/next.png'
344 border='0' height='32' alt='Next Page' width='32' /></A></td>
345<td align="center" width="100%">Python Library Reference</td>
346<td class='online-navigation'><a rel="contents" title="Table of Contents"
347 href="contents.html"><img src='../icons/contents.png'
348 border='0' height='32' alt='Contents' width='32' /></A></td>
349<td class='online-navigation'><a href="modindex.html" title="Module Index"><img src='../icons/modules.png'
350 border='0' height='32' alt='Module Index' width='32' /></a></td>
351<td class='online-navigation'><a rel="index" title="Index"
352 href="genindex.html"><img src='../icons/index.png'
353 border='0' height='32' alt='Index' width='32' /></A></td>
354</tr></table>
355<div class='online-navigation'>
356<b class="navlabel">Previous:</b>
357<a class="sectref" rel="prev" href="optparse-other-reasons-to-extend-optparse.html">6.21.5.3 Other reasons to</A>
358<b class="navlabel">Up:</b>
359<a class="sectref" rel="parent" href="allos.html">6. Generic Operating System</A>
360<b class="navlabel">Next:</b>
361<a class="sectref" rel="next" href="module-errno.html">6.23 errno </A>
362</div>
363</div>
364<hr />
365<span class="release-info">Release 2.4.2, documentation updated on 28 September 2005.</span>
366</DIV>
367<!--End of Navigation Panel-->
368<ADDRESS>
369See <i><a href="about.html">About this document...</a></i> for information on suggesting changes.
370</ADDRESS>
371</BODY>
372</HTML>