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 |
|
---|
11 | This module generates temporary files and directories. It works on
|
---|
12 | all supported platforms.
|
---|
13 |
|
---|
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.
|
---|
21 |
|
---|
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.
|
---|
28 |
|
---|
29 | The 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}}}}}}
|
---|
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.
|
---|
42 |
|
---|
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.
|
---|
48 |
|
---|
49 | The \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}}}}}}
|
---|
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}
|
---|
66 |
|
---|
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.
|
---|
76 |
|
---|
77 | Unlike \function{TemporaryFile()}, the user of \function{mkstemp()} is
|
---|
78 | responsible for deleting the temporary file when done with it.
|
---|
79 |
|
---|
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}.
|
---|
84 |
|
---|
85 | If \var{prefix} is specified, the file name will begin with that
|
---|
86 | prefix; otherwise, a default prefix is used.
|
---|
87 |
|
---|
88 | If \var{dir} is specified, the file will be created in that directory;
|
---|
89 | otherwise, a default directory is used.
|
---|
90 |
|
---|
91 | If \var{text} is specified, it indicates whether to open the file in
|
---|
92 | binary mode (the default) or text mode. On some platforms, this makes
|
---|
93 | no difference.
|
---|
94 |
|
---|
95 | \function{mkstemp()} returns a tuple containing an OS-level handle to
|
---|
96 | an open file (as would be returned by \function{os.open()}) and the
|
---|
97 | absolute 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}}}}
|
---|
102 | Creates a temporary directory in the most secure manner possible.
|
---|
103 | There are no race conditions in the directory's creation. The
|
---|
104 | directory is readable, writable, and searchable only by the
|
---|
105 | creating user ID.
|
---|
106 |
|
---|
107 | The user of \function{mkdtemp()} is responsible for deleting the
|
---|
108 | temporary directory and its contents when done with it.
|
---|
109 |
|
---|
110 | The \var{prefix}, \var{suffix}, and \var{dir} arguments are the same
|
---|
111 | as 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.}
|
---|
119 | Return an absolute pathname of a file that did not exist at the time
|
---|
120 | the call is made. The \var{prefix}, \var{suffix}, and \var{dir}
|
---|
121 | arguments are the same as for \function{mkstemp()}.
|
---|
122 |
|
---|
123 | \warning{Use of this function may introduce a security hole in your
|
---|
124 | program. By the time you get around to doing anything with the file
|
---|
125 | name it returns, someone else may have beaten you to the punch.}
|
---|
126 | \end{funcdesc}
|
---|
127 |
|
---|
128 | The module uses two global variables that tell it how to construct a
|
---|
129 | temporary name. They are initialized at the first call to any of the
|
---|
130 | functions above. The caller may change them, but this is discouraged;
|
---|
131 | use the appropriate function arguments, instead.
|
---|
132 |
|
---|
133 | \begin{datadesc}{tempdir}
|
---|
134 | When set to a value other than \code{None}, this variable defines the
|
---|
135 | default value for the \var{dir} argument to all the functions defined
|
---|
136 | in this module.
|
---|
137 |
|
---|
138 | If \code{tempdir} is unset or \code{None} at any call to any of the
|
---|
139 | above functions, Python searches a standard list of directories and
|
---|
140 | sets \var{tempdir} to the first one which the calling user can create
|
---|
141 | files 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}{}
|
---|
164 | Return the directory currently selected to create temporary files in.
|
---|
165 | If \code{tempdir} is not \code{None}, this simply returns its contents;
|
---|
166 | otherwise, the search described above is performed, and the result
|
---|
167 | returned.
|
---|
168 | \end{funcdesc}
|
---|
169 |
|
---|
170 | \begin{datadesc}{template}
|
---|
171 | \deprecated{2.0}{Use \function{gettempprefix()} instead.}
|
---|
172 | When set to a value other than \code{None}, this variable defines the
|
---|
173 | prefix of the final component of the filenames returned by
|
---|
174 | \function{mktemp()}. A string of six random letters and digits is
|
---|
175 | appended to the prefix to make the filename unique. On Windows,
|
---|
176 | the default prefix is \file{\textasciitilde{}T}; on all other systems
|
---|
177 | it is \file{tmp}.
|
---|
178 |
|
---|
179 | Older versions of this module used to require that \code{template} be
|
---|
180 | set to \code{None} after a call to \function{os.fork()}; this has not
|
---|
181 | been necessary since version 1.5.2.
|
---|
182 | \end{datadesc}
|
---|
183 |
|
---|
184 | \begin{funcdesc}{gettempprefix}{}
|
---|
185 | Return the filename prefix used to create temporary files. This does
|
---|
186 | not contain the directory component. Using this function is preferred
|
---|
187 | over reading the \var{template} variable directly.
|
---|
188 | \versionadded{1.5.2}
|
---|
189 | \end{funcdesc}
|
---|