blob: f9421da5fe7dfab84e3331fc8d9a4b0b8f186760 [file] [log] [blame]
Georg Brandl116aa622007-08-15 14:28:22 +00001:mod:`tempfile` --- Generate temporary files and directories
2============================================================
3
Georg Brandl116aa622007-08-15 14:28:22 +00004.. module:: tempfile
5 :synopsis: Generate temporary files and directories.
6
Terry Jan Reedyfa089b92016-06-11 15:02:54 -04007.. sectionauthor:: Zack Weinberg <zack@codesourcery.com>
8
9**Source code:** :source:`Lib/tempfile.py`
Georg Brandl116aa622007-08-15 14:28:22 +000010
11.. index::
12 pair: temporary; file name
13 pair: temporary; file
14
Raymond Hettingera1993682011-01-27 01:20:32 +000015--------------
16
Robert Collins2ebdc132015-08-13 11:38:02 +120017This module creates temporary files and directories. It works on all
18supported platforms. :class:`TemporaryFile`, :class:`NamedTemporaryFile`,
19:class:`TemporaryDirectory`, and :class:`SpooledTemporaryFile` are high-level
20interfaces which provide automatic cleanup and can be used as
21context managers. :func:`mkstemp` and
22:func:`mkdtemp` are lower-level functions which require manual cleanup.
Georg Brandl116aa622007-08-15 14:28:22 +000023
Robert Collins2ebdc132015-08-13 11:38:02 +120024All the user-callable functions and constructors take additional arguments which
25allow direct control over the location and name of temporary files and
26directories. Files names used by this module include a string of
27random characters which allows those files to be securely created in
28shared temporary directories.
Christian Heimes81ee3ef2008-05-04 22:42:01 +000029To 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
Nick Coghlan543af752010-10-24 11:23:25 +000032The module defines the following user-callable items:
Georg Brandl116aa622007-08-15 14:28:22 +000033
Sergey Fedoseevb48389d2020-09-13 22:59:01 +050034.. function:: TemporaryFile(mode='w+b', buffering=-1, encoding=None, newline=None, suffix=None, prefix=None, dir=None, *, errors=None)
Georg Brandl116aa622007-08-15 14:28:22 +000035
Antoine Pitrou11cb9612010-09-15 11:11:28 +000036 Return a :term:`file-like object` that can be used as a temporary storage area.
Robert Collins2ebdc132015-08-13 11:38:02 +120037 The file is created securely, using the same rules as :func:`mkstemp`. It will be destroyed as soon
Georg Brandl116aa622007-08-15 14:28:22 +000038 as it is closed (including an implicit close when the object is garbage
Robert Collins2ebdc132015-08-13 11:38:02 +120039 collected). Under Unix, the directory entry for the file is either not created at all or is removed
Christian Heimes81ee3ef2008-05-04 22:42:01 +000040 immediately after the file is created. Other platforms do not support
41 this; your code should not rely on a temporary file created using this
42 function having or not having a visible name in the file system.
Georg Brandl116aa622007-08-15 14:28:22 +000043
Robert Collins2ebdc132015-08-13 11:38:02 +120044 The resulting object can be used as a context manager (see
45 :ref:`tempfile-examples`). On completion of the context or
46 destruction of the file object the temporary file will be removed
47 from the filesystem.
48
Christian Heimes81ee3ef2008-05-04 22:42:01 +000049 The *mode* parameter defaults to ``'w+b'`` so that the file created can
50 be read and written without being closed. Binary mode is used so that it
51 behaves consistently on all platforms without regard for the data that is
sth825aab92018-05-23 07:07:01 +020052 stored. *buffering*, *encoding*, *errors* and *newline* are interpreted as for
Georg Brandl14dfede2010-05-21 21:12:07 +000053 :func:`open`.
Georg Brandl116aa622007-08-15 14:28:22 +000054
Martin Panter9b566c32015-11-07 00:32:50 +000055 The *dir*, *prefix* and *suffix* parameters have the same meaning and
56 defaults as with :func:`mkstemp`.
Georg Brandl116aa622007-08-15 14:28:22 +000057
Christian Heimes7f044312008-01-06 17:05:40 +000058 The returned object is a true file object on POSIX platforms. On other
Georg Brandl502d9a52009-07-26 15:02:41 +000059 platforms, it is a file-like object whose :attr:`!file` attribute is the
Robert Collins2ebdc132015-08-13 11:38:02 +120060 underlying true file object.
Christian Heimes7f044312008-01-06 17:05:40 +000061
Victor Stinnerd967fc92014-06-05 14:27:45 +020062 The :py:data:`os.O_TMPFILE` flag is used if it is available and works
Robert Collins2ebdc132015-08-13 11:38:02 +120063 (Linux-specific, requires Linux kernel 3.11 or later).
Victor Stinnerd967fc92014-06-05 14:27:45 +020064
Steve Dower44f91c32019-06-27 10:47:59 -070065 .. audit-event:: tempfile.mkstemp fullpath tempfile.TemporaryFile
Steve Dower60419a72019-06-24 08:42:54 -070066
Victor Stinnerd967fc92014-06-05 14:27:45 +020067 .. versionchanged:: 3.5
68
69 The :py:data:`os.O_TMPFILE` flag is now used if available.
70
sth825aab92018-05-23 07:07:01 +020071 .. versionchanged:: 3.8
72 Added *errors* parameter.
Georg Brandl116aa622007-08-15 14:28:22 +000073
sth825aab92018-05-23 07:07:01 +020074
Sergey Fedoseevb48389d2020-09-13 22:59:01 +050075.. function:: NamedTemporaryFile(mode='w+b', buffering=-1, encoding=None, newline=None, suffix=None, prefix=None, dir=None, delete=True, *, errors=None)
Georg Brandl116aa622007-08-15 14:28:22 +000076
Christian Heimes81ee3ef2008-05-04 22:42:01 +000077 This function operates exactly as :func:`TemporaryFile` does, except that
78 the file is guaranteed to have a visible name in the file system (on
79 Unix, the directory entry is not unlinked). That name can be retrieved
Martin Panter1f0e1f32016-02-22 10:10:00 +000080 from the :attr:`name` attribute of the returned
81 file-like object. Whether the name can be
Christian Heimes81ee3ef2008-05-04 22:42:01 +000082 used to open the file a second time, while the named temporary file is
83 still open, varies across platforms (it can be so used on Unix; it cannot
84 on Windows NT or later). If *delete* is true (the default), the file is
85 deleted as soon as it is closed.
Georg Brandl502d9a52009-07-26 15:02:41 +000086 The returned object is always a file-like object whose :attr:`!file`
Christian Heimes81ee3ef2008-05-04 22:42:01 +000087 attribute is the underlying true file object. This file-like object can
88 be used in a :keyword:`with` statement, just like a normal file.
Georg Brandl116aa622007-08-15 14:28:22 +000089
Steve Dower44f91c32019-06-27 10:47:59 -070090 .. audit-event:: tempfile.mkstemp fullpath tempfile.NamedTemporaryFile
Steve Dower60419a72019-06-24 08:42:54 -070091
sth825aab92018-05-23 07:07:01 +020092 .. versionchanged:: 3.8
93 Added *errors* parameter.
Georg Brandl116aa622007-08-15 14:28:22 +000094
sth825aab92018-05-23 07:07:01 +020095
Sergey Fedoseevb48389d2020-09-13 22:59:01 +050096.. function:: SpooledTemporaryFile(max_size=0, mode='w+b', buffering=-1, encoding=None, newline=None, suffix=None, prefix=None, dir=None, *, errors=None)
Georg Brandl116aa622007-08-15 14:28:22 +000097
Christian Heimes81ee3ef2008-05-04 22:42:01 +000098 This function operates exactly as :func:`TemporaryFile` does, except that
99 data is spooled in memory until the file size exceeds *max_size*, or
100 until the file's :func:`fileno` method is called, at which point the
101 contents are written to disk and operation proceeds as with
102 :func:`TemporaryFile`.
Georg Brandl116aa622007-08-15 14:28:22 +0000103
Christian Heimes81ee3ef2008-05-04 22:42:01 +0000104 The resulting file has one additional method, :func:`rollover`, which
105 causes the file to roll over to an on-disk file regardless of its size.
Georg Brandl116aa622007-08-15 14:28:22 +0000106
Christian Heimes81ee3ef2008-05-04 22:42:01 +0000107 The returned object is a file-like object whose :attr:`_file` attribute
Inada Naokiea9835c2019-11-27 22:22:06 +0900108 is either an :class:`io.BytesIO` or :class:`io.TextIOWrapper` object
109 (depending on whether binary or text *mode* was specified) or a true file
Serhiy Storchaka4f169a72013-02-09 11:46:42 +0200110 object, depending on whether :func:`rollover` has been called. This
111 file-like object can be used in a :keyword:`with` statement, just like
112 a normal file.
Christian Heimes81ee3ef2008-05-04 22:42:01 +0000113
R David Murrayca76ea12012-10-06 18:32:39 -0400114 .. versionchanged:: 3.3
115 the truncate method now accepts a ``size`` argument.
116
sth825aab92018-05-23 07:07:01 +0200117 .. versionchanged:: 3.8
118 Added *errors* parameter.
119
Christian Heimes81ee3ef2008-05-04 22:42:01 +0000120
Martin Panter9b566c32015-11-07 00:32:50 +0000121.. function:: TemporaryDirectory(suffix=None, prefix=None, dir=None)
Nick Coghlan543af752010-10-24 11:23:25 +0000122
Robert Collins2ebdc132015-08-13 11:38:02 +1200123 This function securely creates a temporary directory using the same rules as :func:`mkdtemp`.
Nick Coghlan543af752010-10-24 11:23:25 +0000124 The resulting object can be used as a context manager (see
Robert Collins2ebdc132015-08-13 11:38:02 +1200125 :ref:`tempfile-examples`). On completion of the context or destruction
R David Murray23686072014-02-05 14:53:40 -0500126 of the temporary directory object the newly created temporary directory
Nick Coghlan543af752010-10-24 11:23:25 +0000127 and all its contents are removed from the filesystem.
128
R David Murray23686072014-02-05 14:53:40 -0500129 The directory name can be retrieved from the :attr:`name` attribute of the
130 returned object. When the returned object is used as a context manager, the
Serhiy Storchaka2b57c432018-12-19 08:09:46 +0200131 :attr:`name` will be assigned to the target of the :keyword:`!as` clause in
R David Murray23686072014-02-05 14:53:40 -0500132 the :keyword:`with` statement, if there is one.
Nick Coghlan543af752010-10-24 11:23:25 +0000133
134 The directory can be explicitly cleaned up by calling the
135 :func:`cleanup` method.
136
Steve Dower44f91c32019-06-27 10:47:59 -0700137 .. audit-event:: tempfile.mkdtemp fullpath tempfile.TemporaryDirectory
Steve Dower60419a72019-06-24 08:42:54 -0700138
Nick Coghlan543af752010-10-24 11:23:25 +0000139 .. versionadded:: 3.2
140
141
Gregory P. Smithad577b92015-05-22 16:18:14 -0700142.. function:: mkstemp(suffix=None, prefix=None, dir=None, text=False)
Christian Heimes81ee3ef2008-05-04 22:42:01 +0000143
144 Creates a temporary file in the most secure manner possible. There are
145 no race conditions in the file's creation, assuming that the platform
146 properly implements the :const:`os.O_EXCL` flag for :func:`os.open`. The
147 file is readable and writable only by the creating user ID. If the
148 platform uses permission bits to indicate whether a file is executable,
149 the file is executable by no one. The file descriptor is not inherited
150 by child processes.
151
152 Unlike :func:`TemporaryFile`, the user of :func:`mkstemp` is responsible
153 for deleting the temporary file when done with it.
154
Martin Panter9b566c32015-11-07 00:32:50 +0000155 If *suffix* is not ``None``, the file name will end with that suffix,
Christian Heimes81ee3ef2008-05-04 22:42:01 +0000156 otherwise there will be no suffix. :func:`mkstemp` does not put a dot
157 between the file name and the suffix; if you need one, put it at the
158 beginning of *suffix*.
159
Martin Panter9b566c32015-11-07 00:32:50 +0000160 If *prefix* is not ``None``, the file name will begin with that prefix;
161 otherwise, a default prefix is used. The default is the return value of
162 :func:`gettempprefix` or :func:`gettempprefixb`, as appropriate.
Christian Heimes81ee3ef2008-05-04 22:42:01 +0000163
Martin Panter9b566c32015-11-07 00:32:50 +0000164 If *dir* is not ``None``, the file will be created in that directory;
Christian Heimes81ee3ef2008-05-04 22:42:01 +0000165 otherwise, a default directory is used. The default directory is chosen
166 from a platform-dependent list, but the user of the application can
167 control the directory location by setting the *TMPDIR*, *TEMP* or *TMP*
168 environment variables. There is thus no guarantee that the generated
169 filename will have any nice properties, such as not requiring quoting
170 when passed to external commands via ``os.popen()``.
171
Martin Panter9b566c32015-11-07 00:32:50 +0000172 If any of *suffix*, *prefix*, and *dir* are not
173 ``None``, they must be the same type.
Gregory P. Smithad577b92015-05-22 16:18:14 -0700174 If they are bytes, the returned name will be bytes instead of str.
175 If you want to force a bytes return value with otherwise default behavior,
176 pass ``suffix=b''``.
177
Rishav Kundue55de682020-08-14 07:03:14 +0530178 If *text* is specified and true, the file is opened in text mode.
179 Otherwise, (the default) the file is opened in binary mode.
Christian Heimes81ee3ef2008-05-04 22:42:01 +0000180
181 :func:`mkstemp` returns a tuple containing an OS-level handle to an open
182 file (as would be returned by :func:`os.open`) and the absolute pathname
183 of that file, in that order.
184
Steve Dower44f91c32019-06-27 10:47:59 -0700185 .. audit-event:: tempfile.mkstemp fullpath tempfile.mkstemp
Steve Dower60419a72019-06-24 08:42:54 -0700186
Gregory P. Smithad577b92015-05-22 16:18:14 -0700187 .. versionchanged:: 3.5
188 *suffix*, *prefix*, and *dir* may now be supplied in bytes in order to
189 obtain a bytes return value. Prior to this, only str was allowed.
190 *suffix* and *prefix* now accept and default to ``None`` to cause
191 an appropriate default value to be used.
Christian Heimes81ee3ef2008-05-04 22:42:01 +0000192
Anthony Sottile370138b2019-09-09 08:54:34 -0700193 .. versionchanged:: 3.6
194 The *dir* parameter now accepts a :term:`path-like object`.
195
Gregory P. Smithad577b92015-05-22 16:18:14 -0700196
197.. function:: mkdtemp(suffix=None, prefix=None, dir=None)
Christian Heimes81ee3ef2008-05-04 22:42:01 +0000198
199 Creates a temporary directory in the most secure manner possible. There
200 are no race conditions in the directory's creation. The directory is
201 readable, writable, and searchable only by the creating user ID.
202
203 The user of :func:`mkdtemp` is responsible for deleting the temporary
204 directory and its contents when done with it.
205
206 The *prefix*, *suffix*, and *dir* arguments are the same as for
207 :func:`mkstemp`.
Georg Brandl116aa622007-08-15 14:28:22 +0000208
209 :func:`mkdtemp` returns the absolute pathname of the new directory.
210
Steve Dower44f91c32019-06-27 10:47:59 -0700211 .. audit-event:: tempfile.mkdtemp fullpath tempfile.mkdtemp
Steve Dower60419a72019-06-24 08:42:54 -0700212
Gregory P. Smithad577b92015-05-22 16:18:14 -0700213 .. versionchanged:: 3.5
214 *suffix*, *prefix*, and *dir* may now be supplied in bytes in order to
215 obtain a bytes return value. Prior to this, only str was allowed.
216 *suffix* and *prefix* now accept and default to ``None`` to cause
217 an appropriate default value to be used.
218
Anthony Sottile370138b2019-09-09 08:54:34 -0700219 .. versionchanged:: 3.6
220 The *dir* parameter now accepts a :term:`path-like object`.
221
Georg Brandl116aa622007-08-15 14:28:22 +0000222
Robert Collins2ebdc132015-08-13 11:38:02 +1200223.. function:: gettempdir()
Georg Brandl116aa622007-08-15 14:28:22 +0000224
Robert Collins2ebdc132015-08-13 11:38:02 +1200225 Return the name of the directory used for temporary files. This
226 defines the default value for the *dir* argument to all functions
227 in this module.
Georg Brandl116aa622007-08-15 14:28:22 +0000228
Robert Collins2ebdc132015-08-13 11:38:02 +1200229 Python searches a standard list of directories to find one which
230 the calling user can create files in. The list is:
Georg Brandl116aa622007-08-15 14:28:22 +0000231
232 #. The directory named by the :envvar:`TMPDIR` environment variable.
233
234 #. The directory named by the :envvar:`TEMP` environment variable.
235
236 #. The directory named by the :envvar:`TMP` environment variable.
237
238 #. A platform-specific location:
239
Georg Brandl116aa622007-08-15 14:28:22 +0000240 * On Windows, the directories :file:`C:\\TEMP`, :file:`C:\\TMP`,
241 :file:`\\TEMP`, and :file:`\\TMP`, in that order.
242
243 * On all other platforms, the directories :file:`/tmp`, :file:`/var/tmp`, and
244 :file:`/usr/tmp`, in that order.
245
246 #. As a last resort, the current working directory.
247
Robert Collins2ebdc132015-08-13 11:38:02 +1200248 The result of this search is cached, see the description of
249 :data:`tempdir` below.
Georg Brandl116aa622007-08-15 14:28:22 +0000250
Gregory P. Smithad577b92015-05-22 16:18:14 -0700251.. function:: gettempdirb()
252
253 Same as :func:`gettempdir` but the return value is in bytes.
254
255 .. versionadded:: 3.5
Georg Brandl116aa622007-08-15 14:28:22 +0000256
Georg Brandl116aa622007-08-15 14:28:22 +0000257.. function:: gettempprefix()
258
259 Return the filename prefix used to create temporary files. This does not
Georg Brandl4b26ff82008-08-04 07:24:52 +0000260 contain the directory component.
Georg Brandl116aa622007-08-15 14:28:22 +0000261
Gregory P. Smithad577b92015-05-22 16:18:14 -0700262.. function:: gettempprefixb()
263
Martin Panter9b566c32015-11-07 00:32:50 +0000264 Same as :func:`gettempprefix` but the return value is in bytes.
Gregory P. Smithad577b92015-05-22 16:18:14 -0700265
266 .. versionadded:: 3.5
267
Robert Collins2ebdc132015-08-13 11:38:02 +1200268The module uses a global variable to store the name of the directory
269used for temporary files returned by :func:`gettempdir`. It can be
270set directly to override the selection process, but this is discouraged.
271All functions in this module take a *dir* argument which can be used
Jelle Zijlstrad3b8f982017-03-11 09:34:55 -0800272to specify the directory and this is the recommended approach.
Robert Collins2ebdc132015-08-13 11:38:02 +1200273
274.. data:: tempdir
275
276 When set to a value other than ``None``, this variable defines the
Jelle Zijlstrad3b8f982017-03-11 09:34:55 -0800277 default value for the *dir* argument to the functions defined in this
Robert Collins2ebdc132015-08-13 11:38:02 +1200278 module.
279
Srinivas Reddy Thatiparthy (శ్రీనివాస్ రెడ్డి తాటిపర్తి)940ae602018-06-21 13:22:43 +0530280 If ``tempdir`` is ``None`` (the default) at any call to any of the above
Martin Panter8f265652016-04-19 04:03:41 +0000281 functions except :func:`gettempprefix` it is initialized following the
Robert Collins2ebdc132015-08-13 11:38:02 +1200282 algorithm described in :func:`gettempdir`.
283
284.. _tempfile-examples:
Nick Coghlan543af752010-10-24 11:23:25 +0000285
286Examples
287--------
288
289Here are some examples of typical usage of the :mod:`tempfile` module::
290
291 >>> import tempfile
292
293 # create a temporary file and write some data to it
294 >>> fp = tempfile.TemporaryFile()
Ross Lagerwall810b94a2011-04-10 09:30:04 +0200295 >>> fp.write(b'Hello world!')
Nick Coghlan543af752010-10-24 11:23:25 +0000296 # read data from file
297 >>> fp.seek(0)
298 >>> fp.read()
Ross Lagerwall810b94a2011-04-10 09:30:04 +0200299 b'Hello world!'
Nick Coghlan543af752010-10-24 11:23:25 +0000300 # close the file, it will be removed
301 >>> fp.close()
302
303 # create a temporary file using a context manager
304 >>> with tempfile.TemporaryFile() as fp:
Ross Lagerwall810b94a2011-04-10 09:30:04 +0200305 ... fp.write(b'Hello world!')
Nick Coghlan543af752010-10-24 11:23:25 +0000306 ... fp.seek(0)
307 ... fp.read()
Ross Lagerwall810b94a2011-04-10 09:30:04 +0200308 b'Hello world!'
Nick Coghlan543af752010-10-24 11:23:25 +0000309 >>>
310 # file is now closed and removed
311
312 # create a temporary directory using the context manager
313 >>> with tempfile.TemporaryDirectory() as tmpdirname:
Ross Lagerwall810b94a2011-04-10 09:30:04 +0200314 ... print('created temporary directory', tmpdirname)
Nick Coghlan543af752010-10-24 11:23:25 +0000315 >>>
316 # directory and contents have been removed
317
Robert Collins2ebdc132015-08-13 11:38:02 +1200318
319Deprecated functions and variables
320----------------------------------
321
322A historical way to create temporary files was to first generate a
323file name with the :func:`mktemp` function and then create a file
324using this name. Unfortunately this is not secure, because a different
325process may create a file with this name in the time between the call
326to :func:`mktemp` and the subsequent attempt to create the file by the
327first process. The solution is to combine the two steps and create the
328file immediately. This approach is used by :func:`mkstemp` and the
329other functions described above.
330
331.. function:: mktemp(suffix='', prefix='tmp', dir=None)
332
333 .. deprecated:: 2.3
334 Use :func:`mkstemp` instead.
335
336 Return an absolute pathname of a file that did not exist at the time the
Martin Panter9b566c32015-11-07 00:32:50 +0000337 call is made. The *prefix*, *suffix*, and *dir* arguments are similar
338 to those of :func:`mkstemp`, except that bytes file names, ``suffix=None``
339 and ``prefix=None`` are not supported.
Robert Collins2ebdc132015-08-13 11:38:02 +1200340
341 .. warning::
342
343 Use of this function may introduce a security hole in your program. By
344 the time you get around to doing anything with the file name it returns,
345 someone else may have beaten you to the punch. :func:`mktemp` usage can
346 be replaced easily with :func:`NamedTemporaryFile`, passing it the
347 ``delete=False`` parameter::
348
349 >>> f = NamedTemporaryFile(delete=False)
350 >>> f.name
351 '/tmp/tmptjujjt'
352 >>> f.write(b"Hello World!\n")
353 13
354 >>> f.close()
355 >>> os.unlink(f.name)
356 >>> os.path.exists(f.name)
357 False