blob: 0387fb142036694d2aa936b1995dbe0817ef94c3 [file] [log] [blame]
Georg Brandl116aa622007-08-15 14:28:22 +00001:mod:`tempfile` --- Generate temporary files and directories
2============================================================
3
4.. sectionauthor:: Zack Weinberg <zack@codesourcery.com>
5
6
7.. module:: tempfile
8 :synopsis: Generate temporary files and directories.
9
10
11.. index::
12 pair: temporary; file name
13 pair: temporary; file
14
Raymond Hettingera1993682011-01-27 01:20:32 +000015**Source code:** :source:`Lib/tempfile.py`
16
17--------------
18
Georg Brandl116aa622007-08-15 14:28:22 +000019This module generates temporary files and directories. It works on all
Georg Brandle6bcc912008-05-12 18:05:20 +000020supported platforms. It provides three new functions,
21:func:`NamedTemporaryFile`, :func:`mkstemp`, and :func:`mkdtemp`, which should
22eliminate all remaining need to use the insecure :func:`mktemp` function.
23Temporary file names created by this module no longer contain the process ID;
24instead a string of six random characters is used.
Georg Brandl116aa622007-08-15 14:28:22 +000025
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
R David Murray3a420c72011-06-22 21:01:13 -040028no longer necessary to use the global *tempdir* variable.
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
Georg Brandl14dfede2010-05-21 21:12:07 +000034.. function:: TemporaryFile(mode='w+b', buffering=None, encoding=None, newline=None, suffix='', prefix='tmp', dir=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.
Christian Heimes81ee3ef2008-05-04 22:42:01 +000037 The file is created using :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
Christian Heimes81ee3ef2008-05-04 22:42:01 +000039 collected). Under Unix, the directory entry for the file is removed
40 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
Christian Heimes81ee3ef2008-05-04 22:42:01 +000044 The *mode* parameter defaults to ``'w+b'`` so that the file created can
45 be read and written without being closed. Binary mode is used so that it
46 behaves consistently on all platforms without regard for the data that is
Georg Brandl14dfede2010-05-21 21:12:07 +000047 stored. *buffering*, *encoding* and *newline* are interpreted as for
48 :func:`open`.
Georg Brandl116aa622007-08-15 14:28:22 +000049
50 The *dir*, *prefix* and *suffix* parameters are passed to :func:`mkstemp`.
51
Christian Heimes7f044312008-01-06 17:05:40 +000052 The returned object is a true file object on POSIX platforms. On other
Georg Brandl502d9a52009-07-26 15:02:41 +000053 platforms, it is a file-like object whose :attr:`!file` attribute is the
Christian Heimes81ee3ef2008-05-04 22:42:01 +000054 underlying true file object. This file-like object can be used in a
Christian Heimes3ecfea712008-02-09 20:51:34 +000055 :keyword:`with` statement, just like a normal file.
Christian Heimes7f044312008-01-06 17:05:40 +000056
Victor Stinnerd967fc92014-06-05 14:27:45 +020057 The :py:data:`os.O_TMPFILE` flag is used if it is available and works
58 (Linux-specific, require Linux kernel 3.11 or later).
59
60 .. versionchanged:: 3.5
61
62 The :py:data:`os.O_TMPFILE` flag is now used if available.
63
Georg Brandl116aa622007-08-15 14:28:22 +000064
Georg Brandl14dfede2010-05-21 21:12:07 +000065.. function:: NamedTemporaryFile(mode='w+b', buffering=None, encoding=None, newline=None, suffix='', prefix='tmp', dir=None, delete=True)
Georg Brandl116aa622007-08-15 14:28:22 +000066
Christian Heimes81ee3ef2008-05-04 22:42:01 +000067 This function operates exactly as :func:`TemporaryFile` does, except that
68 the file is guaranteed to have a visible name in the file system (on
69 Unix, the directory entry is not unlinked). That name can be retrieved
Senthil Kumarana6bac952011-07-04 11:28:30 -070070 from the :attr:`name` attribute of the file object. Whether the name can be
Christian Heimes81ee3ef2008-05-04 22:42:01 +000071 used to open the file a second time, while the named temporary file is
72 still open, varies across platforms (it can be so used on Unix; it cannot
73 on Windows NT or later). If *delete* is true (the default), the file is
74 deleted as soon as it is closed.
Georg Brandl502d9a52009-07-26 15:02:41 +000075 The returned object is always a file-like object whose :attr:`!file`
Christian Heimes81ee3ef2008-05-04 22:42:01 +000076 attribute is the underlying true file object. This file-like object can
77 be used in a :keyword:`with` statement, just like a normal file.
Georg Brandl116aa622007-08-15 14:28:22 +000078
Georg Brandl116aa622007-08-15 14:28:22 +000079
Georg Brandl14dfede2010-05-21 21:12:07 +000080.. function:: SpooledTemporaryFile(max_size=0, mode='w+b', buffering=None, encoding=None, newline=None, suffix='', prefix='tmp', dir=None)
Georg Brandl116aa622007-08-15 14:28:22 +000081
Christian Heimes81ee3ef2008-05-04 22:42:01 +000082 This function operates exactly as :func:`TemporaryFile` does, except that
83 data is spooled in memory until the file size exceeds *max_size*, or
84 until the file's :func:`fileno` method is called, at which point the
85 contents are written to disk and operation proceeds as with
86 :func:`TemporaryFile`.
Georg Brandl116aa622007-08-15 14:28:22 +000087
Christian Heimes81ee3ef2008-05-04 22:42:01 +000088 The resulting file has one additional method, :func:`rollover`, which
89 causes the file to roll over to an on-disk file regardless of its size.
Georg Brandl116aa622007-08-15 14:28:22 +000090
Christian Heimes81ee3ef2008-05-04 22:42:01 +000091 The returned object is a file-like object whose :attr:`_file` attribute
Serhiy Storchakae79be872013-08-17 00:09:55 +030092 is either a :class:`io.BytesIO` or :class:`io.StringIO` object (depending on
Serhiy Storchaka4b109cb2013-02-09 11:51:21 +020093 whether binary or text *mode* was specified) or a true file
Serhiy Storchaka4f169a72013-02-09 11:46:42 +020094 object, depending on whether :func:`rollover` has been called. This
95 file-like object can be used in a :keyword:`with` statement, just like
96 a normal file.
Christian Heimes81ee3ef2008-05-04 22:42:01 +000097
R David Murrayca76ea12012-10-06 18:32:39 -040098 .. versionchanged:: 3.3
99 the truncate method now accepts a ``size`` argument.
100
Christian Heimes81ee3ef2008-05-04 22:42:01 +0000101
Nick Coghlan543af752010-10-24 11:23:25 +0000102.. function:: TemporaryDirectory(suffix='', prefix='tmp', dir=None)
103
104 This function creates a temporary directory using :func:`mkdtemp`
105 (the supplied arguments are passed directly to the underlying function).
106 The resulting object can be used as a context manager (see
R David Murray23686072014-02-05 14:53:40 -0500107 :ref:`context-managers`). On completion of the context or destruction
108 of the temporary directory object the newly created temporary directory
Nick Coghlan543af752010-10-24 11:23:25 +0000109 and all its contents are removed from the filesystem.
110
R David Murray23686072014-02-05 14:53:40 -0500111 The directory name can be retrieved from the :attr:`name` attribute of the
112 returned object. When the returned object is used as a context manager, the
113 :attr:`name` will be assigned to the target of the :keyword:`as` clause in
114 the :keyword:`with` statement, if there is one.
Nick Coghlan543af752010-10-24 11:23:25 +0000115
116 The directory can be explicitly cleaned up by calling the
117 :func:`cleanup` method.
118
119 .. versionadded:: 3.2
120
121
Gregory P. Smithad577b92015-05-22 16:18:14 -0700122.. function:: mkstemp(suffix=None, prefix=None, dir=None, text=False)
Christian Heimes81ee3ef2008-05-04 22:42:01 +0000123
124 Creates a temporary file in the most secure manner possible. There are
125 no race conditions in the file's creation, assuming that the platform
126 properly implements the :const:`os.O_EXCL` flag for :func:`os.open`. The
127 file is readable and writable only by the creating user ID. If the
128 platform uses permission bits to indicate whether a file is executable,
129 the file is executable by no one. The file descriptor is not inherited
130 by child processes.
131
132 Unlike :func:`TemporaryFile`, the user of :func:`mkstemp` is responsible
133 for deleting the temporary file when done with it.
134
135 If *suffix* is specified, the file name will end with that suffix,
136 otherwise there will be no suffix. :func:`mkstemp` does not put a dot
137 between the file name and the suffix; if you need one, put it at the
138 beginning of *suffix*.
139
140 If *prefix* is specified, the file name will begin with that prefix;
141 otherwise, a default prefix is used.
142
143 If *dir* is specified, the file will be created in that directory;
144 otherwise, a default directory is used. The default directory is chosen
145 from a platform-dependent list, but the user of the application can
146 control the directory location by setting the *TMPDIR*, *TEMP* or *TMP*
147 environment variables. There is thus no guarantee that the generated
148 filename will have any nice properties, such as not requiring quoting
149 when passed to external commands via ``os.popen()``.
150
Gregory P. Smithad577b92015-05-22 16:18:14 -0700151 *suffix*, *prefix*, and *dir* must all contain the same type, if specified.
152 If they are bytes, the returned name will be bytes instead of str.
153 If you want to force a bytes return value with otherwise default behavior,
154 pass ``suffix=b''``.
155
156 A *prefix* value of ``None`` means use the return value of
157 :func:`gettempprefix` or :func:`gettempprefixb` as appropriate.
158
159 A *suffix* value of ``None`` means use an appropriate empty value.
160
Christian Heimes81ee3ef2008-05-04 22:42:01 +0000161 If *text* is specified, it indicates whether to open the file in binary
162 mode (the default) or text mode. On some platforms, this makes no
163 difference.
164
165 :func:`mkstemp` returns a tuple containing an OS-level handle to an open
166 file (as would be returned by :func:`os.open`) and the absolute pathname
167 of that file, in that order.
168
Gregory P. Smithad577b92015-05-22 16:18:14 -0700169 .. versionchanged:: 3.5
170 *suffix*, *prefix*, and *dir* may now be supplied in bytes in order to
171 obtain a bytes return value. Prior to this, only str was allowed.
172 *suffix* and *prefix* now accept and default to ``None`` to cause
173 an appropriate default value to be used.
Christian Heimes81ee3ef2008-05-04 22:42:01 +0000174
Gregory P. Smithad577b92015-05-22 16:18:14 -0700175
176.. function:: mkdtemp(suffix=None, prefix=None, dir=None)
Christian Heimes81ee3ef2008-05-04 22:42:01 +0000177
178 Creates a temporary directory in the most secure manner possible. There
179 are no race conditions in the directory's creation. The directory is
180 readable, writable, and searchable only by the creating user ID.
181
182 The user of :func:`mkdtemp` is responsible for deleting the temporary
183 directory and its contents when done with it.
184
185 The *prefix*, *suffix*, and *dir* arguments are the same as for
186 :func:`mkstemp`.
Georg Brandl116aa622007-08-15 14:28:22 +0000187
188 :func:`mkdtemp` returns the absolute pathname of the new directory.
189
Gregory P. Smithad577b92015-05-22 16:18:14 -0700190 .. versionchanged:: 3.5
191 *suffix*, *prefix*, and *dir* may now be supplied in bytes in order to
192 obtain a bytes return value. Prior to this, only str was allowed.
193 *suffix* and *prefix* now accept and default to ``None`` to cause
194 an appropriate default value to be used.
195
Georg Brandl116aa622007-08-15 14:28:22 +0000196
Georg Brandl7f01a132009-09-16 15:58:14 +0000197.. function:: mktemp(suffix='', prefix='tmp', dir=None)
Georg Brandl116aa622007-08-15 14:28:22 +0000198
199 .. deprecated:: 2.3
200 Use :func:`mkstemp` instead.
201
Christian Heimes81ee3ef2008-05-04 22:42:01 +0000202 Return an absolute pathname of a file that did not exist at the time the
203 call is made. The *prefix*, *suffix*, and *dir* arguments are the same
204 as for :func:`mkstemp`.
Georg Brandl116aa622007-08-15 14:28:22 +0000205
206 .. warning::
207
Georg Brandl36ab1ef2009-01-03 21:17:04 +0000208 Use of this function may introduce a security hole in your program. By
209 the time you get around to doing anything with the file name it returns,
210 someone else may have beaten you to the punch. :func:`mktemp` usage can
211 be replaced easily with :func:`NamedTemporaryFile`, passing it the
212 ``delete=False`` parameter::
Alexandre Vassalotti6461e102008-05-15 22:09:29 +0000213
214 >>> f = NamedTemporaryFile(delete=False)
Alexandre Vassalotti6461e102008-05-15 22:09:29 +0000215 >>> f.name
Ezio Melottiad17bc02013-02-22 08:28:14 +0200216 '/tmp/tmptjujjt'
217 >>> f.write(b"Hello World!\n")
218 13
Alexandre Vassalotti6461e102008-05-15 22:09:29 +0000219 >>> f.close()
220 >>> os.unlink(f.name)
221 >>> os.path.exists(f.name)
222 False
Georg Brandl116aa622007-08-15 14:28:22 +0000223
Georg Brandl8ed75cd2014-10-31 10:25:48 +0100224The module uses a global variable that tell it how to construct a
Christian Heimes81ee3ef2008-05-04 22:42:01 +0000225temporary name. They are initialized at the first call to any of the
226functions above. The caller may change them, but this is discouraged; use
227the appropriate function arguments, instead.
Georg Brandl116aa622007-08-15 14:28:22 +0000228
229
230.. data:: tempdir
231
Christian Heimes81ee3ef2008-05-04 22:42:01 +0000232 When set to a value other than ``None``, this variable defines the
233 default value for the *dir* argument to all the functions defined in this
234 module.
Georg Brandl116aa622007-08-15 14:28:22 +0000235
Christian Heimes81ee3ef2008-05-04 22:42:01 +0000236 If ``tempdir`` is unset or ``None`` at any call to any of the above
237 functions, Python searches a standard list of directories and sets
238 *tempdir* to the first one which the calling user can create files in.
239 The list is:
Georg Brandl116aa622007-08-15 14:28:22 +0000240
241 #. The directory named by the :envvar:`TMPDIR` environment variable.
242
243 #. The directory named by the :envvar:`TEMP` environment variable.
244
245 #. The directory named by the :envvar:`TMP` environment variable.
246
247 #. A platform-specific location:
248
Georg Brandl116aa622007-08-15 14:28:22 +0000249 * On Windows, the directories :file:`C:\\TEMP`, :file:`C:\\TMP`,
250 :file:`\\TEMP`, and :file:`\\TMP`, in that order.
251
252 * On all other platforms, the directories :file:`/tmp`, :file:`/var/tmp`, and
253 :file:`/usr/tmp`, in that order.
254
255 #. As a last resort, the current working directory.
256
257
258.. function:: gettempdir()
259
260 Return the directory currently selected to create temporary files in. If
261 :data:`tempdir` is not ``None``, this simply returns its contents; otherwise,
262 the search described above is performed, and the result returned.
263
Gregory P. Smithad577b92015-05-22 16:18:14 -0700264.. function:: gettempdirb()
265
266 Same as :func:`gettempdir` but the return value is in bytes.
267
268 .. versionadded:: 3.5
Georg Brandl116aa622007-08-15 14:28:22 +0000269
Georg Brandl116aa622007-08-15 14:28:22 +0000270.. function:: gettempprefix()
271
272 Return the filename prefix used to create temporary files. This does not
Georg Brandl4b26ff82008-08-04 07:24:52 +0000273 contain the directory component.
Georg Brandl116aa622007-08-15 14:28:22 +0000274
Gregory P. Smithad577b92015-05-22 16:18:14 -0700275.. function:: gettempprefixb()
276
277 Same as :func:`gettempprefixb` but the return value is in bytes.
278
279 .. versionadded:: 3.5
280
Nick Coghlan543af752010-10-24 11:23:25 +0000281
282Examples
283--------
284
285Here are some examples of typical usage of the :mod:`tempfile` module::
286
287 >>> import tempfile
288
289 # create a temporary file and write some data to it
290 >>> fp = tempfile.TemporaryFile()
Ross Lagerwall810b94a2011-04-10 09:30:04 +0200291 >>> fp.write(b'Hello world!')
Nick Coghlan543af752010-10-24 11:23:25 +0000292 # read data from file
293 >>> fp.seek(0)
294 >>> fp.read()
Ross Lagerwall810b94a2011-04-10 09:30:04 +0200295 b'Hello world!'
Nick Coghlan543af752010-10-24 11:23:25 +0000296 # close the file, it will be removed
297 >>> fp.close()
298
299 # create a temporary file using a context manager
300 >>> with tempfile.TemporaryFile() as fp:
Ross Lagerwall810b94a2011-04-10 09:30:04 +0200301 ... fp.write(b'Hello world!')
Nick Coghlan543af752010-10-24 11:23:25 +0000302 ... fp.seek(0)
303 ... fp.read()
Ross Lagerwall810b94a2011-04-10 09:30:04 +0200304 b'Hello world!'
Nick Coghlan543af752010-10-24 11:23:25 +0000305 >>>
306 # file is now closed and removed
307
308 # create a temporary directory using the context manager
309 >>> with tempfile.TemporaryDirectory() as tmpdirname:
Ross Lagerwall810b94a2011-04-10 09:30:04 +0200310 ... print('created temporary directory', tmpdirname)
Nick Coghlan543af752010-10-24 11:23:25 +0000311 >>>
312 # directory and contents have been removed
313