blob: 8b778e92e06bca9b00539a785fd495ce5a265986 [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,
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.
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
Guido van Rossum830a5152002-08-09 16:16:30 +000031\begin{funcdesc}{TemporaryFile}{\optional{mode='w+b'}
32 \optional{, bufsize=-1}
33 \optional{, suffix}
34 \optional{, prefix}
35 \optional{, dir}}
Fred Drake02330752000-05-26 19:05:16 +000036Return a file (or file-like) object that can be used as a temporary
Guido van Rossum830a5152002-08-09 16:16:30 +000037storage area. The file is created using \function{mkstemp}. It will
38be destroyed as soon as it is closed (including an implicit close when
39the object is garbage collected). Under \UNIX, the directory entry
40for the file is removed immediately after the file is created. Other
41platforms do not support this; your code should not rely on a
42\class{TemporaryFile} having or not having a visible name in the file
43system.
Fred Drake02330752000-05-26 19:05:16 +000044
45The \var{mode} parameter defaults to \code{'w+b'} so that the file
46created can be read and written without being closed. Binary mode is
47used so that it behaves consistently on all platforms without regard
48for the data that is stored. \var{bufsize} defaults to \code{-1},
Guido van Rossum830a5152002-08-09 16:16:30 +000049meaning that the operating system default is used.
50
51The \var{dir}, \var{prefix} and \var{suffix} parameters are passed to
52\function{mkstemp}.
53\end{funcdesc}
54
55\begin{funcdesc}{NamedTemporaryFile}{\optional{mode='w+b'}
56 \optional{, bufsize=-1}
57 \optional{, suffix}
58 \optional{, prefix}
59 \optional{, dir}}
60This function operates exactly as \function{TemporaryFile} does,
61except that the file is guaranteed to have a visible name in the file
62system. That name can be retrieved from the \member{name} member of
63the file object.
64\versionadded{2.3}
65\end{funcdesc}
66
67\begin{funcdesc}{mkstemp}{\optional{suffix}
68 \optional{, prefix}
69 \optional{, dir}
70 \optional{, binary=1}}
71Creates a temporary file in the most secure manner possible. There
72are no race conditions in the file's creation, assuming that the
73platform properly implements the \constant{O_EXCL} flag for
74\function{os.open}. The file is readable and writable only by the
75creating user ID. If the platform uses permission bits to indicate
76whether a file is executable, the file is executable by no one. The
77file descriptor is not inherited by child processes.
78
79Unlike \function{TemporaryFile}, the user of \function{mkstemp} is
80responsible for deleting the temporary file when done with it.
81
82If \var{suffix} is specified, the file name will end with that suffix,
83otherwise there will be no suffix. \function{mkstemp} does not put a
84dot between the file name and the suffix; if you need one, put it at
85the beginning of \var{suffix}.
86
87If \var{prefix} is specified, the file name will begin with that
88prefix; otherwise, a default prefix is used.
89
90If \var{dir} is specified, the file will be created in that directory;
91otherwise, a default directory is used.
92
93If \var{binary} is specified, it indicates whether to open the file in
94binary mode (the default) or text mode. On some platforms, this makes
95no difference.
96
97\function{mkstemp} returns a tuple containing an OS-level handle to
98an open file (as would be returned by \function{os.open}) and the
99absolute pathname of that file, in that order.
100\versionadded{2.3}
101\end{funcdesc}
102
103\begin{funcdesc}{mkdtemp}{\optional{suffix}
104 \optional{, prefix}
105 \optional{, dir}}
106Creates a temporary directory in the most secure manner possible.
107There are no race conditions in the directory's creation. The
108directory is readable, writable, and searchable only by the
109creating user ID.
110
111The user of \function{mkdtemp} is responsible for deleting the
112temporary directory and its contents when done with it.
113
114The \var{prefix}, \var{suffix}, and \var{dir} arguments are the same
115as for \function{mkstemp}.
116
117\function{mkdtemp} returns the absolute pathname of the new directory.
118\versionadded{2.3}
119\end{funcdesc}
120
121\begin{funcdesc}{mktemp}{\optional{suffix}
122 \optional{, prefix}
123 \optional{, dir}}
124\deprecated{2.3}{Use \function{mkstemp()} instead.}
125Return an absolute pathname of a file that did not exist at the time
126the call is made. The \var{prefix}, \var{suffix}, and \var{dir}
127arguments are the same as for \function{mkstemp}.
128
129\warning{Use of this function may introduce a security hole in your
130program. By the time you get around to doing anything with the file
131name it returns, someone else may have beaten you to the punch.}
Guido van Rossum81b30601995-03-01 14:36:00 +0000132\end{funcdesc}
133
134The module uses two global variables that tell it how to construct a
Guido van Rossum830a5152002-08-09 16:16:30 +0000135temporary name. They are initialized at the first call to any of the
136functions above. The caller may change them, but this is discouraged;
137use the appropriate function arguments, instead.
Guido van Rossum81b30601995-03-01 14:36:00 +0000138
139\begin{datadesc}{tempdir}
140When set to a value other than \code{None}, this variable defines the
Guido van Rossum830a5152002-08-09 16:16:30 +0000141default value for the \var{dir} argument to all the functions defined
142in this module.
143
144If \var{tempdir} is unset or \code{None} at any call to any of the
145above functions, Python searches a standard list of directories and
146sets \var{tempdir} to the first one which the calling user can create
147files in. The list is:
148
149\begin{enumerate}
150\item The directory named by the \envvar{TMPDIR} environment variable.
151\item The directory named by the \envvar{TEMP} environment variable.
152\item The directory named by the \envvar{TMP} environment variable.
153\item A platform-specific location:
154 \begin{itemize}
155 \item On Macintosh, the \file{Temporary Items} folder.
156 \item On RiscOS, the directory named by the
157 \envvar{Wimp\$ScrapDir} environment variable.
158 \item On Windows, the directories
159 \file{C:$\backslash$TEMP},
160 \file{C:$\backslash$TMP},
161 \file{$\backslash$TEMP}, and
162 \file{$\backslash$TMP}, in that order.
163 \item On all other platforms, the directories
164 \file{/tmp}, \file{/var/tmp}, and \file{/usr/tmp}, in that order.
165 \end{itemize}
166\item As a last resort, the current working directory.
167\end{enumerate}
Guido van Rossum81b30601995-03-01 14:36:00 +0000168\end{datadesc}
Guido van Rossum81b30601995-03-01 14:36:00 +0000169
Guido van Rossum830a5152002-08-09 16:16:30 +0000170\begin{funcdesc}{gettempdir}{}
171Return the directory currently selected to create temporary files in.
172If \var{tempdir} is not None, this simply returns its contents;
173otherwise, the search described above is performed, and the result
174returned.
Fred Drakeb80a7772000-05-26 19:32:14 +0000175\end{funcdesc}
176
Guido van Rossum81b30601995-03-01 14:36:00 +0000177\begin{datadesc}{template}
Fred Drake30f76ff2000-06-30 16:06:19 +0000178\deprecated{2.0}{Use \function{gettempprefix()} instead.}
Guido van Rossum81b30601995-03-01 14:36:00 +0000179When set to a value other than \code{None}, this variable defines the
180prefix of the final component of the filenames returned by
Guido van Rossum830a5152002-08-09 16:16:30 +0000181\function{mktemp()}. A string of six random letters and digits is
182appended to the prefix to make the filename unique. On Windows,
183the default prefix is \file{\textasciitilde{}T}; on all other systems
184it is \file{tmp}.
Fred Drakeb80a7772000-05-26 19:32:14 +0000185
186Older versions of this module used to require that \code{template} be
187set to \code{None} after a call to \function{os.fork()}; this has not
188been necessary since version 1.5.2.
Guido van Rossum81b30601995-03-01 14:36:00 +0000189\end{datadesc}
Guido van Rossum830a5152002-08-09 16:16:30 +0000190
191\begin{funcdesc}{gettempprefix}{}
192Return the filename prefix used to create temporary files. This does
193not contain the directory component. Using this function is preferred
194over reading the \var{template} variable directly.
195\versionadded{1.5.2}
196\end{funcdesc}