blob: 4de9236df848caf4807f0a2fd9065d76cddd96c8 [file] [log] [blame]
Georg Brandl116aa622007-08-15 14:28:22 +00001
2:mod:`tempfile` --- Generate temporary files and directories
3============================================================
4
5.. sectionauthor:: Zack Weinberg <zack@codesourcery.com>
6
7
8.. module:: tempfile
9 :synopsis: Generate temporary files and directories.
10
11
12.. index::
13 pair: temporary; file name
14 pair: temporary; file
15
16This module generates temporary files and directories. It works on all
17supported platforms.
18
19In version 2.3 of Python, this module was overhauled for enhanced security. It
20now provides three new functions, :func:`NamedTemporaryFile`, :func:`mkstemp`,
21and :func:`mkdtemp`, which should eliminate all remaining need to use the
22insecure :func:`mktemp` function. Temporary file names created by this module
23no longer contain the process ID; instead a string of six random characters is
24used.
25
Christian Heimes81ee3ef2008-05-04 22:42:01 +000026Also, all the user-callable functions now take additional arguments which
27allow direct control over the location and name of temporary files. It is
28no longer necessary to use the global *tempdir* and *template* variables.
29To maintain backward compatibility, the argument order is somewhat odd; it
30is recommended to use keyword arguments for clarity.
Georg Brandl116aa622007-08-15 14:28:22 +000031
32The module defines the following user-callable functions:
33
34
Christian Heimes81ee3ef2008-05-04 22:42:01 +000035.. function:: TemporaryFile([mode='w+b'[, bufsize=-1[, suffix=''[, prefix='tmp'[, dir=None]]]]])
Georg Brandl116aa622007-08-15 14:28:22 +000036
Christian Heimes81ee3ef2008-05-04 22:42:01 +000037 Return a file-like object that can be used as a temporary storage area.
38 The file is created using :func:`mkstemp`. It will be destroyed as soon
Georg Brandl116aa622007-08-15 14:28:22 +000039 as it is closed (including an implicit close when the object is garbage
Christian Heimes81ee3ef2008-05-04 22:42:01 +000040 collected). Under Unix, the directory entry for the file is removed
41 immediately after the file is created. Other platforms do not support
42 this; your code should not rely on a temporary file created using this
43 function having or not having a visible name in the file system.
Georg Brandl116aa622007-08-15 14:28:22 +000044
Christian Heimes81ee3ef2008-05-04 22:42:01 +000045 The *mode* parameter defaults to ``'w+b'`` so that the file created can
46 be read and written without being closed. Binary mode is used so that it
47 behaves consistently on all platforms without regard for the data that is
48 stored. *bufsize* defaults to ``-1``, meaning that the operating system
49 default is used.
Georg Brandl116aa622007-08-15 14:28:22 +000050
51 The *dir*, *prefix* and *suffix* parameters are passed to :func:`mkstemp`.
52
Christian Heimes7f044312008-01-06 17:05:40 +000053 The returned object is a true file object on POSIX platforms. On other
54 platforms, it is a file-like object whose :attr:`file` attribute is the
Christian Heimes81ee3ef2008-05-04 22:42:01 +000055 underlying true file object. This file-like object can be used in a
Christian Heimes3ecfea712008-02-09 20:51:34 +000056 :keyword:`with` statement, just like a normal file.
Christian Heimes7f044312008-01-06 17:05:40 +000057
Georg Brandl116aa622007-08-15 14:28:22 +000058
Christian Heimes81ee3ef2008-05-04 22:42:01 +000059.. function:: NamedTemporaryFile([mode='w+b'[, bufsize=-1[, suffix=''[, prefix='tmp'[, dir=None[, delete=True]]]]]])
Georg Brandl116aa622007-08-15 14:28:22 +000060
Christian Heimes81ee3ef2008-05-04 22:42:01 +000061 This function operates exactly as :func:`TemporaryFile` does, except that
62 the file is guaranteed to have a visible name in the file system (on
63 Unix, the directory entry is not unlinked). That name can be retrieved
64 from the :attr:`name` member of the file object. Whether the name can be
65 used to open the file a second time, while the named temporary file is
66 still open, varies across platforms (it can be so used on Unix; it cannot
67 on Windows NT or later). If *delete* is true (the default), the file is
68 deleted as soon as it is closed.
69 The returned object is always a file-like object whose :attr:`file`
70 attribute is the underlying true file object. This file-like object can
71 be used in a :keyword:`with` statement, just like a normal file.
Georg Brandl116aa622007-08-15 14:28:22 +000072
Georg Brandl116aa622007-08-15 14:28:22 +000073
Christian Heimes81ee3ef2008-05-04 22:42:01 +000074.. function:: SpooledTemporaryFile([max_size=0, [mode='w+b'[, bufsize=-1[, suffix=''[, prefix='tmp'[, dir=None]]]]]])
Georg Brandl116aa622007-08-15 14:28:22 +000075
Christian Heimes81ee3ef2008-05-04 22:42:01 +000076 This function operates exactly as :func:`TemporaryFile` does, except that
77 data is spooled in memory until the file size exceeds *max_size*, or
78 until the file's :func:`fileno` method is called, at which point the
79 contents are written to disk and operation proceeds as with
80 :func:`TemporaryFile`.
Georg Brandl116aa622007-08-15 14:28:22 +000081
Christian Heimes81ee3ef2008-05-04 22:42:01 +000082 The resulting file has one additional method, :func:`rollover`, which
83 causes the file to roll over to an on-disk file regardless of its size.
Georg Brandl116aa622007-08-15 14:28:22 +000084
Christian Heimes81ee3ef2008-05-04 22:42:01 +000085 The returned object is a file-like object whose :attr:`_file` attribute
86 is either a :class:`StringIO` object or a true file object, depending on
87 whether :func:`rollover` has been called. This file-like object can be
88 used in a :keyword:`with` statement, just like a normal file.
89
90
91.. function:: mkstemp([suffix=''[, prefix='tmp'[, dir=None[, text=False]]]])
92
93 Creates a temporary file in the most secure manner possible. There are
94 no race conditions in the file's creation, assuming that the platform
95 properly implements the :const:`os.O_EXCL` flag for :func:`os.open`. The
96 file is readable and writable only by the creating user ID. If the
97 platform uses permission bits to indicate whether a file is executable,
98 the file is executable by no one. The file descriptor is not inherited
99 by child processes.
100
101 Unlike :func:`TemporaryFile`, the user of :func:`mkstemp` is responsible
102 for deleting the temporary file when done with it.
103
104 If *suffix* is specified, the file name will end with that suffix,
105 otherwise there will be no suffix. :func:`mkstemp` does not put a dot
106 between the file name and the suffix; if you need one, put it at the
107 beginning of *suffix*.
108
109 If *prefix* is specified, the file name will begin with that prefix;
110 otherwise, a default prefix is used.
111
112 If *dir* is specified, the file will be created in that directory;
113 otherwise, a default directory is used. The default directory is chosen
114 from a platform-dependent list, but the user of the application can
115 control the directory location by setting the *TMPDIR*, *TEMP* or *TMP*
116 environment variables. There is thus no guarantee that the generated
117 filename will have any nice properties, such as not requiring quoting
118 when passed to external commands via ``os.popen()``.
119
120 If *text* is specified, it indicates whether to open the file in binary
121 mode (the default) or text mode. On some platforms, this makes no
122 difference.
123
124 :func:`mkstemp` returns a tuple containing an OS-level handle to an open
125 file (as would be returned by :func:`os.open`) and the absolute pathname
126 of that file, in that order.
127
128
129.. function:: mkdtemp([suffix=''[, prefix='tmp'[, dir=None]]])
130
131 Creates a temporary directory in the most secure manner possible. There
132 are no race conditions in the directory's creation. The directory is
133 readable, writable, and searchable only by the creating user ID.
134
135 The user of :func:`mkdtemp` is responsible for deleting the temporary
136 directory and its contents when done with it.
137
138 The *prefix*, *suffix*, and *dir* arguments are the same as for
139 :func:`mkstemp`.
Georg Brandl116aa622007-08-15 14:28:22 +0000140
141 :func:`mkdtemp` returns the absolute pathname of the new directory.
142
Georg Brandl116aa622007-08-15 14:28:22 +0000143
Christian Heimes81ee3ef2008-05-04 22:42:01 +0000144.. function:: mktemp([suffix=''[, prefix='tmp'[, dir=None]]])
Georg Brandl116aa622007-08-15 14:28:22 +0000145
146 .. deprecated:: 2.3
147 Use :func:`mkstemp` instead.
148
Christian Heimes81ee3ef2008-05-04 22:42:01 +0000149 Return an absolute pathname of a file that did not exist at the time the
150 call is made. The *prefix*, *suffix*, and *dir* arguments are the same
151 as for :func:`mkstemp`.
Georg Brandl116aa622007-08-15 14:28:22 +0000152
153 .. warning::
154
Christian Heimes81ee3ef2008-05-04 22:42:01 +0000155 Use of this function may introduce a security hole in your program.
156 By the time you get around to doing anything with the file name it
157 returns, someone else may have beaten you to the punch.
Georg Brandl116aa622007-08-15 14:28:22 +0000158
Christian Heimes81ee3ef2008-05-04 22:42:01 +0000159The module uses two global variables that tell it how to construct a
160temporary name. They are initialized at the first call to any of the
161functions above. The caller may change them, but this is discouraged; use
162the appropriate function arguments, instead.
Georg Brandl116aa622007-08-15 14:28:22 +0000163
164
165.. data:: tempdir
166
Christian Heimes81ee3ef2008-05-04 22:42:01 +0000167 When set to a value other than ``None``, this variable defines the
168 default value for the *dir* argument to all the functions defined in this
169 module.
Georg Brandl116aa622007-08-15 14:28:22 +0000170
Christian Heimes81ee3ef2008-05-04 22:42:01 +0000171 If ``tempdir`` is unset or ``None`` at any call to any of the above
172 functions, Python searches a standard list of directories and sets
173 *tempdir* to the first one which the calling user can create files in.
174 The list is:
Georg Brandl116aa622007-08-15 14:28:22 +0000175
176 #. The directory named by the :envvar:`TMPDIR` environment variable.
177
178 #. The directory named by the :envvar:`TEMP` environment variable.
179
180 #. The directory named by the :envvar:`TMP` environment variable.
181
182 #. A platform-specific location:
183
Georg Brandl116aa622007-08-15 14:28:22 +0000184 * On Windows, the directories :file:`C:\\TEMP`, :file:`C:\\TMP`,
185 :file:`\\TEMP`, and :file:`\\TMP`, in that order.
186
187 * On all other platforms, the directories :file:`/tmp`, :file:`/var/tmp`, and
188 :file:`/usr/tmp`, in that order.
189
190 #. As a last resort, the current working directory.
191
192
193.. function:: gettempdir()
194
195 Return the directory currently selected to create temporary files in. If
196 :data:`tempdir` is not ``None``, this simply returns its contents; otherwise,
197 the search described above is performed, and the result returned.
198
199
200.. data:: template
201
202 .. deprecated:: 2.0
203 Use :func:`gettempprefix` instead.
204
205 When set to a value other than ``None``, this variable defines the prefix of the
206 final component of the filenames returned by :func:`mktemp`. A string of six
207 random letters and digits is appended to the prefix to make the filename unique.
208 On Windows, the default prefix is :file:`~T`; on all other systems it is
209 :file:`tmp`.
210
211 Older versions of this module used to require that ``template`` be set to
212 ``None`` after a call to :func:`os.fork`; this has not been necessary since
213 version 1.5.2.
214
215
216.. function:: gettempprefix()
217
218 Return the filename prefix used to create temporary files. This does not
219 contain the directory component. Using this function is preferred over reading
220 the *template* variable directly.
221