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