blob: 8bc559e34ed5e754f32f95511607477c6e4eb8e7 [file] [log] [blame]
Fred Drake295da241998-08-10 19:42:37 +00001\section{\module{tempfile} ---
Guido van Rossum830a5152002-08-09 16:16:30 +00002 Generate temporary files and directories}
3\sectionauthor{Zack Weinberg}{zack@codesourcery.com}
Fred Drakeb91e9341998-07-23 17:59:49 +00004
Fred Drake737aa551999-04-21 17:01:15 +00005\declaremodule{standard}{tempfile}
Guido van Rossum830a5152002-08-09 16:16:30 +00006\modulesynopsis{Generate temporary files and directories.}
Fred Drakeb91e9341998-07-23 17:59:49 +00007
Guido van Rossum81b30601995-03-01 14:36:00 +00008\indexii{temporary}{file name}
9\indexii{temporary}{file}
10
Guido van Rossum830a5152002-08-09 16:16:30 +000011This module generates temporary files and directories. It works on
12all supported platforms.
Guido van Rossum81b30601995-03-01 14:36:00 +000013
Guido van Rossum830a5152002-08-09 16:16:30 +000014In version 2.3 of Python, this module was overhauled for enhanced
15security. It now provides three new functions,
Fred Drake70995832003-04-22 18:54:53 +000016\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
Guido van Rossum830a5152002-08-09 16:16:30 +000019by 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.
Guido van Rossum81b30601995-03-01 14:36:00 +000028
Fred Drake02330752000-05-26 19:05:16 +000029The module defines the following user-callable functions:
Guido van Rossum81b30601995-03-01 14:36:00 +000030
Fred Drake614438a2003-09-11 18:18:54 +000031\begin{funcdesc}{TemporaryFile}{\optional{mode=\code{'w+b'}\optional{,
32 bufsize=\code{-1}\optional{,
33 suffix\optional{, prefix\optional{, dir}}}}}}
Fred Drake02330752000-05-26 19:05:16 +000034Return a file (or file-like) object that can be used as a temporary
Guido van Rossum830a5152002-08-09 16:16:30 +000035storage 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
Fred Drake70995832003-04-22 18:54:53 +000040temporary file created using this function having or not having a
41visible name in the file system.
Fred Drake02330752000-05-26 19:05:16 +000042
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},
Guido van Rossum830a5152002-08-09 16:16:30 +000047meaning that the operating system default is used.
48
49The \var{dir}, \var{prefix} and \var{suffix} parameters are passed to
Fred Drake70995832003-04-22 18:54:53 +000050\function{mkstemp()}.
Guido van Rossum830a5152002-08-09 16:16:30 +000051\end{funcdesc}
52
Fred Drake614438a2003-09-11 18:18:54 +000053\begin{funcdesc}{NamedTemporaryFile}{\optional{mode=\code{'w+b'}\optional{,
54 bufsize=\code{-1}\optional{,
55 suffix\optional{, prefix\optional{,
Guido van Rossumd8faa362007-04-27 19:54:29 +000056 dir\optional{, delete}}}}}}}
Fred Drake70995832003-04-22 18:54:53 +000057This function operates exactly as \function{TemporaryFile()} does,
Guido van Rossum830a5152002-08-09 16:16:30 +000058except that the file is guaranteed to have a visible name in the file
Tim Peters3350b5b2002-11-21 16:32:11 +000059system (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).
Guido van Rossumd8faa362007-04-27 19:54:29 +000064If \var{delete} is true (the default), the file is deleted as soon as
65it is closed.
Guido van Rossum830a5152002-08-09 16:16:30 +000066\versionadded{2.3}
Guido van Rossumd8faa362007-04-27 19:54:29 +000067\versionadded[The \var{delete} parameter]{2.6}
68\end{funcdesc}
69
70\begin{funcdesc}{SpooledTemporaryFile}{\optional{max\_size=\code{0},
71 \optional{mode=\code{'w+b'}\optional{,
72 bufsize=\code{-1}\optional{,
73 suffix\optional{, prefix\optional{,
74 dir}}}}}}}
75This function operates exactly as \function{TemporaryFile()} does,
76except that data is spooled in memory until the file size exceeds
77\var{max_size}, or until the file's \function{fileno()} method is
78called, at which point the contents are written to disk and operation
79proceeds as with \function{TemporaryFile()}.
80
81The resulting file has one additional method, \function{rollover()},
82which causes the file to roll over to an on-disk file regardless of
83its size.
84\versionadded{2.6}
Guido van Rossum830a5152002-08-09 16:16:30 +000085\end{funcdesc}
86
Fred Drake614438a2003-09-11 18:18:54 +000087\begin{funcdesc}{mkstemp}{\optional{suffix\optional{,
88 prefix\optional{, dir\optional{, text}}}}}
Guido van Rossum830a5152002-08-09 16:16:30 +000089Creates a temporary file in the most secure manner possible. There
90are no race conditions in the file's creation, assuming that the
91platform properly implements the \constant{O_EXCL} flag for
Fred Drake70995832003-04-22 18:54:53 +000092\function{os.open()}. The file is readable and writable only by the
Guido van Rossum830a5152002-08-09 16:16:30 +000093creating user ID. If the platform uses permission bits to indicate
94whether a file is executable, the file is executable by no one. The
95file descriptor is not inherited by child processes.
96
Fred Drake70995832003-04-22 18:54:53 +000097Unlike \function{TemporaryFile()}, the user of \function{mkstemp()} is
Guido van Rossum830a5152002-08-09 16:16:30 +000098responsible for deleting the temporary file when done with it.
99
100If \var{suffix} is specified, the file name will end with that suffix,
Fred Drake70995832003-04-22 18:54:53 +0000101otherwise there will be no suffix. \function{mkstemp()} does not put a
Guido van Rossum830a5152002-08-09 16:16:30 +0000102dot between the file name and the suffix; if you need one, put it at
103the beginning of \var{suffix}.
104
105If \var{prefix} is specified, the file name will begin with that
106prefix; otherwise, a default prefix is used.
107
108If \var{dir} is specified, the file will be created in that directory;
Thomas Wouters89f507f2006-12-13 04:49:30 +0000109otherwise, a default directory is used. The default directory is chosen
110from a platform-dependent list, but the user of the application can control
111the directory location by setting the \var{TMPDIR}, \var{TEMP} or \var{TMP}
112environment variables. There is thus no guarantee that the generated
113filename will have any nice properties, such as not requiring quoting when
114passed to external commands via \code{os.popen()}.
Guido van Rossum830a5152002-08-09 16:16:30 +0000115
Tim Peters2f238c12002-08-14 16:37:10 +0000116If \var{text} is specified, it indicates whether to open the file in
Guido van Rossum830a5152002-08-09 16:16:30 +0000117binary mode (the default) or text mode. On some platforms, this makes
118no difference.
119
Fred Drake70995832003-04-22 18:54:53 +0000120\function{mkstemp()} returns a tuple containing an OS-level handle to
121an open file (as would be returned by \function{os.open()}) and the
Guido van Rossum830a5152002-08-09 16:16:30 +0000122absolute pathname of that file, in that order.
123\versionadded{2.3}
124\end{funcdesc}
125
Fred Drake614438a2003-09-11 18:18:54 +0000126\begin{funcdesc}{mkdtemp}{\optional{suffix\optional{, prefix\optional{, dir}}}}
Guido van Rossum830a5152002-08-09 16:16:30 +0000127Creates a temporary directory in the most secure manner possible.
128There are no race conditions in the directory's creation. The
129directory is readable, writable, and searchable only by the
130creating user ID.
131
Fred Drake70995832003-04-22 18:54:53 +0000132The user of \function{mkdtemp()} is responsible for deleting the
Guido van Rossum830a5152002-08-09 16:16:30 +0000133temporary directory and its contents when done with it.
134
135The \var{prefix}, \var{suffix}, and \var{dir} arguments are the same
Fred Drake70995832003-04-22 18:54:53 +0000136as for \function{mkstemp()}.
Guido van Rossum830a5152002-08-09 16:16:30 +0000137
Fred Drake70995832003-04-22 18:54:53 +0000138\function{mkdtemp()} returns the absolute pathname of the new directory.
Guido van Rossum830a5152002-08-09 16:16:30 +0000139\versionadded{2.3}
140\end{funcdesc}
141
Fred Drake614438a2003-09-11 18:18:54 +0000142\begin{funcdesc}{mktemp}{\optional{suffix\optional{, prefix\optional{, dir}}}}
Guido van Rossum830a5152002-08-09 16:16:30 +0000143\deprecated{2.3}{Use \function{mkstemp()} instead.}
144Return an absolute pathname of a file that did not exist at the time
145the call is made. The \var{prefix}, \var{suffix}, and \var{dir}
Fred Drake70995832003-04-22 18:54:53 +0000146arguments are the same as for \function{mkstemp()}.
Guido van Rossum830a5152002-08-09 16:16:30 +0000147
148\warning{Use of this function may introduce a security hole in your
149program. By the time you get around to doing anything with the file
150name it returns, someone else may have beaten you to the punch.}
Guido van Rossum81b30601995-03-01 14:36:00 +0000151\end{funcdesc}
152
153The module uses two global variables that tell it how to construct a
Guido van Rossum830a5152002-08-09 16:16:30 +0000154temporary name. They are initialized at the first call to any of the
155functions above. The caller may change them, but this is discouraged;
156use the appropriate function arguments, instead.
Guido van Rossum81b30601995-03-01 14:36:00 +0000157
158\begin{datadesc}{tempdir}
159When set to a value other than \code{None}, this variable defines the
Guido van Rossum830a5152002-08-09 16:16:30 +0000160default value for the \var{dir} argument to all the functions defined
161in this module.
162
Fred Drake614438a2003-09-11 18:18:54 +0000163If \code{tempdir} is unset or \code{None} at any call to any of the
Guido van Rossum830a5152002-08-09 16:16:30 +0000164above functions, Python searches a standard list of directories and
165sets \var{tempdir} to the first one which the calling user can create
166files in. The list is:
167
168\begin{enumerate}
169\item The directory named by the \envvar{TMPDIR} environment variable.
170\item The directory named by the \envvar{TEMP} environment variable.
171\item The directory named by the \envvar{TMP} environment variable.
172\item A platform-specific location:
173 \begin{itemize}
Guido van Rossum830a5152002-08-09 16:16:30 +0000174 \item On RiscOS, the directory named by the
175 \envvar{Wimp\$ScrapDir} environment variable.
176 \item On Windows, the directories
177 \file{C:$\backslash$TEMP},
178 \file{C:$\backslash$TMP},
179 \file{$\backslash$TEMP}, and
180 \file{$\backslash$TMP}, in that order.
181 \item On all other platforms, the directories
182 \file{/tmp}, \file{/var/tmp}, and \file{/usr/tmp}, in that order.
183 \end{itemize}
184\item As a last resort, the current working directory.
185\end{enumerate}
Guido van Rossum81b30601995-03-01 14:36:00 +0000186\end{datadesc}
Guido van Rossum81b30601995-03-01 14:36:00 +0000187
Guido van Rossum830a5152002-08-09 16:16:30 +0000188\begin{funcdesc}{gettempdir}{}
189Return the directory currently selected to create temporary files in.
Fred Drake614438a2003-09-11 18:18:54 +0000190If \code{tempdir} is not \code{None}, this simply returns its contents;
Guido van Rossum830a5152002-08-09 16:16:30 +0000191otherwise, the search described above is performed, and the result
192returned.
Fred Drakeb80a7772000-05-26 19:32:14 +0000193\end{funcdesc}
194
Guido van Rossum81b30601995-03-01 14:36:00 +0000195\begin{datadesc}{template}
Fred Drake30f76ff2000-06-30 16:06:19 +0000196\deprecated{2.0}{Use \function{gettempprefix()} instead.}
Guido van Rossum81b30601995-03-01 14:36:00 +0000197When set to a value other than \code{None}, this variable defines the
198prefix of the final component of the filenames returned by
Guido van Rossum830a5152002-08-09 16:16:30 +0000199\function{mktemp()}. A string of six random letters and digits is
200appended to the prefix to make the filename unique. On Windows,
201the default prefix is \file{\textasciitilde{}T}; on all other systems
202it is \file{tmp}.
Fred Drakeb80a7772000-05-26 19:32:14 +0000203
204Older versions of this module used to require that \code{template} be
205set to \code{None} after a call to \function{os.fork()}; this has not
206been necessary since version 1.5.2.
Guido van Rossum81b30601995-03-01 14:36:00 +0000207\end{datadesc}
Guido van Rossum830a5152002-08-09 16:16:30 +0000208
209\begin{funcdesc}{gettempprefix}{}
210Return the filename prefix used to create temporary files. This does
211not contain the directory component. Using this function is preferred
212over reading the \var{template} variable directly.
213\versionadded{1.5.2}
214\end{funcdesc}