source: vendor/python/2.5/Doc/lib/libtempfile.tex

Last change on this file was 3225, checked in by bird, 18 years ago

Python 2.5

File size: 8.3 KB
Line 
1\section{\module{tempfile} ---
2 Generate temporary files and directories}
3\sectionauthor{Zack Weinberg}{zack@codesourcery.com}
4
5\declaremodule{standard}{tempfile}
6\modulesynopsis{Generate temporary files and directories.}
7
8\indexii{temporary}{file name}
9\indexii{temporary}{file}
10
11This module generates temporary files and directories. It works on
12all supported platforms.
13
14In version 2.3 of Python, this module was overhauled for enhanced
15security. It now provides three new functions,
16\function{NamedTemporaryFile()}, \function{mkstemp()}, and
17\function{mkdtemp()}, which should eliminate all remaining need to use
18the insecure \function{mktemp()} function. Temporary file names created
19by this module no longer contain the process ID; instead a string of
20six random characters is used.
21
22Also, all the user-callable functions now take additional arguments
23which allow direct control over the location and name of temporary
24files. It is no longer necessary to use the global \var{tempdir} and
25\var{template} variables. To maintain backward compatibility, the
26argument order is somewhat odd; it is recommended to use keyword
27arguments for clarity.
28
29The module defines the following user-callable functions:
30
31\begin{funcdesc}{TemporaryFile}{\optional{mode=\code{'w+b'}\optional{,
32 bufsize=\code{-1}\optional{,
33 suffix\optional{, prefix\optional{, dir}}}}}}
34Return a file (or file-like) object that can be used as a temporary
35storage area. The file is created using \function{mkstemp}. It will
36be destroyed as soon as it is closed (including an implicit close when
37the object is garbage collected). Under \UNIX, the directory entry
38for the file is removed immediately after the file is created. Other
39platforms do not support this; your code should not rely on a
40temporary file created using this function having or not having a
41visible name in the file system.
42
43The \var{mode} parameter defaults to \code{'w+b'} so that the file
44created can be read and written without being closed. Binary mode is
45used so that it behaves consistently on all platforms without regard
46for the data that is stored. \var{bufsize} defaults to \code{-1},
47meaning that the operating system default is used.
48
49The \var{dir}, \var{prefix} and \var{suffix} parameters are passed to
50\function{mkstemp()}.
51\end{funcdesc}
52
53\begin{funcdesc}{NamedTemporaryFile}{\optional{mode=\code{'w+b'}\optional{,
54 bufsize=\code{-1}\optional{,
55 suffix\optional{, prefix\optional{,
56 dir}}}}}}
57This function operates exactly as \function{TemporaryFile()} does,
58except that the file is guaranteed to have a visible name in the file
59system (on \UNIX, the directory entry is not unlinked). That name can
60be retrieved from the \member{name} member of the file object. Whether
61the name can be used to open the file a second time, while the
62named temporary file is still open, varies across platforms (it can
63be so used on \UNIX; it cannot on Windows NT or later).
64\versionadded{2.3}
65\end{funcdesc}
66
67\begin{funcdesc}{mkstemp}{\optional{suffix\optional{,
68 prefix\optional{, dir\optional{, text}}}}}
69Creates a temporary file in the most secure manner possible. There
70are no race conditions in the file's creation, assuming that the
71platform properly implements the \constant{O_EXCL} flag for
72\function{os.open()}. The file is readable and writable only by the
73creating user ID. If the platform uses permission bits to indicate
74whether a file is executable, the file is executable by no one. The
75file descriptor is not inherited by child processes.
76
77Unlike \function{TemporaryFile()}, the user of \function{mkstemp()} is
78responsible for deleting the temporary file when done with it.
79
80If \var{suffix} is specified, the file name will end with that suffix,
81otherwise there will be no suffix. \function{mkstemp()} does not put a
82dot between the file name and the suffix; if you need one, put it at
83the beginning of \var{suffix}.
84
85If \var{prefix} is specified, the file name will begin with that
86prefix; otherwise, a default prefix is used.
87
88If \var{dir} is specified, the file will be created in that directory;
89otherwise, a default directory is used.
90
91If \var{text} is specified, it indicates whether to open the file in
92binary mode (the default) or text mode. On some platforms, this makes
93no difference.
94
95\function{mkstemp()} returns a tuple containing an OS-level handle to
96an open file (as would be returned by \function{os.open()}) and the
97absolute pathname of that file, in that order.
98\versionadded{2.3}
99\end{funcdesc}
100
101\begin{funcdesc}{mkdtemp}{\optional{suffix\optional{, prefix\optional{, dir}}}}
102Creates a temporary directory in the most secure manner possible.
103There are no race conditions in the directory's creation. The
104directory is readable, writable, and searchable only by the
105creating user ID.
106
107The user of \function{mkdtemp()} is responsible for deleting the
108temporary directory and its contents when done with it.
109
110The \var{prefix}, \var{suffix}, and \var{dir} arguments are the same
111as for \function{mkstemp()}.
112
113\function{mkdtemp()} returns the absolute pathname of the new directory.
114\versionadded{2.3}
115\end{funcdesc}
116
117\begin{funcdesc}{mktemp}{\optional{suffix\optional{, prefix\optional{, dir}}}}
118\deprecated{2.3}{Use \function{mkstemp()} instead.}
119Return an absolute pathname of a file that did not exist at the time
120the call is made. The \var{prefix}, \var{suffix}, and \var{dir}
121arguments are the same as for \function{mkstemp()}.
122
123\warning{Use of this function may introduce a security hole in your
124program. By the time you get around to doing anything with the file
125name it returns, someone else may have beaten you to the punch.}
126\end{funcdesc}
127
128The module uses two global variables that tell it how to construct a
129temporary name. They are initialized at the first call to any of the
130functions above. The caller may change them, but this is discouraged;
131use the appropriate function arguments, instead.
132
133\begin{datadesc}{tempdir}
134When set to a value other than \code{None}, this variable defines the
135default value for the \var{dir} argument to all the functions defined
136in this module.
137
138If \code{tempdir} is unset or \code{None} at any call to any of the
139above functions, Python searches a standard list of directories and
140sets \var{tempdir} to the first one which the calling user can create
141files in. The list is:
142
143\begin{enumerate}
144\item The directory named by the \envvar{TMPDIR} environment variable.
145\item The directory named by the \envvar{TEMP} environment variable.
146\item The directory named by the \envvar{TMP} environment variable.
147\item A platform-specific location:
148 \begin{itemize}
149 \item On RiscOS, the directory named by the
150 \envvar{Wimp\$ScrapDir} environment variable.
151 \item On Windows, the directories
152 \file{C:$\backslash$TEMP},
153 \file{C:$\backslash$TMP},
154 \file{$\backslash$TEMP}, and
155 \file{$\backslash$TMP}, in that order.
156 \item On all other platforms, the directories
157 \file{/tmp}, \file{/var/tmp}, and \file{/usr/tmp}, in that order.
158 \end{itemize}
159\item As a last resort, the current working directory.
160\end{enumerate}
161\end{datadesc}
162
163\begin{funcdesc}{gettempdir}{}
164Return the directory currently selected to create temporary files in.
165If \code{tempdir} is not \code{None}, this simply returns its contents;
166otherwise, the search described above is performed, and the result
167returned.
168\end{funcdesc}
169
170\begin{datadesc}{template}
171\deprecated{2.0}{Use \function{gettempprefix()} instead.}
172When set to a value other than \code{None}, this variable defines the
173prefix of the final component of the filenames returned by
174\function{mktemp()}. A string of six random letters and digits is
175appended to the prefix to make the filename unique. On Windows,
176the default prefix is \file{\textasciitilde{}T}; on all other systems
177it is \file{tmp}.
178
179Older versions of this module used to require that \code{template} be
180set to \code{None} after a call to \function{os.fork()}; this has not
181been necessary since version 1.5.2.
182\end{datadesc}
183
184\begin{funcdesc}{gettempprefix}{}
185Return the filename prefix used to create temporary files. This does
186not contain the directory component. Using this function is preferred
187over reading the \var{template} variable directly.
188\versionadded{1.5.2}
189\end{funcdesc}
Note: See TracBrowser for help on using the repository browser.