| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 1 | :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 Hettinger | a199368 | 2011-01-27 01:20:32 +0000 | [diff] [blame] | 15 | **Source code:** :source:`Lib/tempfile.py` | 
 | 16 |  | 
 | 17 | -------------- | 
 | 18 |  | 
| Robert Collins | 2ebdc13 | 2015-08-13 11:38:02 +1200 | [diff] [blame] | 19 | This module creates temporary files and directories.  It works on all | 
 | 20 | supported platforms. :class:`TemporaryFile`, :class:`NamedTemporaryFile`, | 
 | 21 | :class:`TemporaryDirectory`, and :class:`SpooledTemporaryFile` are high-level | 
 | 22 | interfaces which provide automatic cleanup and can be used as | 
 | 23 | context managers. :func:`mkstemp` and | 
 | 24 | :func:`mkdtemp` are lower-level functions which require manual cleanup. | 
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 25 |  | 
| Robert Collins | 2ebdc13 | 2015-08-13 11:38:02 +1200 | [diff] [blame] | 26 | All the user-callable functions and constructors take additional arguments which | 
 | 27 | allow direct control over the location and name of temporary files and | 
 | 28 | directories. Files names used by this module include a string of | 
 | 29 | random characters which allows those files to be securely created in | 
 | 30 | shared temporary directories. | 
| Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 31 | To maintain backward compatibility, the argument order is somewhat odd; it | 
 | 32 | is recommended to use keyword arguments for clarity. | 
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 33 |  | 
| Nick Coghlan | 543af75 | 2010-10-24 11:23:25 +0000 | [diff] [blame] | 34 | The module defines the following user-callable items: | 
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 35 |  | 
| Martin Panter | 9b566c3 | 2015-11-07 00:32:50 +0000 | [diff] [blame] | 36 | .. function:: TemporaryFile(mode='w+b', buffering=None, encoding=None, newline=None, suffix=None, prefix=None, dir=None) | 
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 37 |  | 
| Antoine Pitrou | 11cb961 | 2010-09-15 11:11:28 +0000 | [diff] [blame] | 38 |    Return a :term:`file-like object` that can be used as a temporary storage area. | 
| Robert Collins | 2ebdc13 | 2015-08-13 11:38:02 +1200 | [diff] [blame] | 39 |    The file is created securely, using the same rules as :func:`mkstemp`. It will be destroyed as soon | 
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 40 |    as it is closed (including an implicit close when the object is garbage | 
| Robert Collins | 2ebdc13 | 2015-08-13 11:38:02 +1200 | [diff] [blame] | 41 |    collected).  Under Unix, the directory entry for the file is either not created at all or is removed | 
| Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 42 |    immediately after the file is created.  Other platforms do not support | 
 | 43 |    this; your code should not rely on a temporary file created using this | 
 | 44 |    function having or not having a visible name in the file system. | 
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 45 |  | 
| Robert Collins | 2ebdc13 | 2015-08-13 11:38:02 +1200 | [diff] [blame] | 46 |    The resulting object can be used as a context manager (see | 
 | 47 |    :ref:`tempfile-examples`).  On completion of the context or | 
 | 48 |    destruction of the file object the temporary file will be removed | 
 | 49 |    from the filesystem. | 
 | 50 |  | 
| Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 51 |    The *mode* parameter defaults to ``'w+b'`` so that the file created can | 
 | 52 |    be read and written without being closed.  Binary mode is used so that it | 
 | 53 |    behaves consistently on all platforms without regard for the data that is | 
| Georg Brandl | 14dfede | 2010-05-21 21:12:07 +0000 | [diff] [blame] | 54 |    stored.  *buffering*, *encoding* and *newline* are interpreted as for | 
 | 55 |    :func:`open`. | 
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 56 |  | 
| Martin Panter | 9b566c3 | 2015-11-07 00:32:50 +0000 | [diff] [blame] | 57 |    The *dir*, *prefix* and *suffix* parameters have the same meaning and | 
 | 58 |    defaults as with :func:`mkstemp`. | 
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 59 |  | 
| Christian Heimes | 7f04431 | 2008-01-06 17:05:40 +0000 | [diff] [blame] | 60 |    The returned object is a true file object on POSIX platforms.  On other | 
| Georg Brandl | 502d9a5 | 2009-07-26 15:02:41 +0000 | [diff] [blame] | 61 |    platforms, it is a file-like object whose :attr:`!file` attribute is the | 
| Robert Collins | 2ebdc13 | 2015-08-13 11:38:02 +1200 | [diff] [blame] | 62 |    underlying true file object. | 
| Christian Heimes | 7f04431 | 2008-01-06 17:05:40 +0000 | [diff] [blame] | 63 |  | 
| Victor Stinner | d967fc9 | 2014-06-05 14:27:45 +0200 | [diff] [blame] | 64 |    The :py:data:`os.O_TMPFILE` flag is used if it is available and works | 
| Robert Collins | 2ebdc13 | 2015-08-13 11:38:02 +1200 | [diff] [blame] | 65 |    (Linux-specific, requires Linux kernel 3.11 or later). | 
| Victor Stinner | d967fc9 | 2014-06-05 14:27:45 +0200 | [diff] [blame] | 66 |  | 
 | 67 |    .. versionchanged:: 3.5 | 
 | 68 |  | 
 | 69 |       The :py:data:`os.O_TMPFILE` flag is now used if available. | 
 | 70 |  | 
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 71 |  | 
| Martin Panter | 9b566c3 | 2015-11-07 00:32:50 +0000 | [diff] [blame] | 72 | .. function:: NamedTemporaryFile(mode='w+b', buffering=None, encoding=None, newline=None, suffix=None, prefix=None, dir=None, delete=True) | 
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 73 |  | 
| Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 74 |    This function operates exactly as :func:`TemporaryFile` does, except that | 
 | 75 |    the file is guaranteed to have a visible name in the file system (on | 
 | 76 |    Unix, the directory entry is not unlinked).  That name can be retrieved | 
| Senthil Kumaran | a6bac95 | 2011-07-04 11:28:30 -0700 | [diff] [blame] | 77 |    from the :attr:`name` attribute of the file object.  Whether the name can be | 
| Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 78 |    used to open the file a second time, while the named temporary file is | 
 | 79 |    still open, varies across platforms (it can be so used on Unix; it cannot | 
 | 80 |    on Windows NT or later).  If *delete* is true (the default), the file is | 
 | 81 |    deleted as soon as it is closed. | 
| Georg Brandl | 502d9a5 | 2009-07-26 15:02:41 +0000 | [diff] [blame] | 82 |    The returned object is always a file-like object whose :attr:`!file` | 
| Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 83 |    attribute is the underlying true file object. This file-like object can | 
 | 84 |    be used in a :keyword:`with` statement, just like a normal file. | 
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 85 |  | 
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 86 |  | 
| Martin Panter | 9b566c3 | 2015-11-07 00:32:50 +0000 | [diff] [blame] | 87 | .. function:: SpooledTemporaryFile(max_size=0, mode='w+b', buffering=None, encoding=None, newline=None, suffix=None, prefix=None, dir=None) | 
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 88 |  | 
| Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 89 |    This function operates exactly as :func:`TemporaryFile` does, except that | 
 | 90 |    data is spooled in memory until the file size exceeds *max_size*, or | 
 | 91 |    until the file's :func:`fileno` method is called, at which point the | 
 | 92 |    contents are written to disk and operation proceeds as with | 
 | 93 |    :func:`TemporaryFile`. | 
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 94 |  | 
| Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 95 |    The resulting file has one additional method, :func:`rollover`, which | 
 | 96 |    causes the file to roll over to an on-disk file regardless of its size. | 
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 97 |  | 
| Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 98 |    The returned object is a file-like object whose :attr:`_file` attribute | 
| Martin Panter | 7462b649 | 2015-11-02 03:37:02 +0000 | [diff] [blame] | 99 |    is either an :class:`io.BytesIO` or :class:`io.StringIO` object (depending on | 
| Serhiy Storchaka | 4b109cb | 2013-02-09 11:51:21 +0200 | [diff] [blame] | 100 |    whether binary or text *mode* was specified) or a true file | 
| Serhiy Storchaka | 4f169a7 | 2013-02-09 11:46:42 +0200 | [diff] [blame] | 101 |    object, depending on whether :func:`rollover` has been called.  This | 
 | 102 |    file-like object can be used in a :keyword:`with` statement, just like | 
 | 103 |    a normal file. | 
| Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 104 |  | 
| R David Murray | ca76ea1 | 2012-10-06 18:32:39 -0400 | [diff] [blame] | 105 |    .. versionchanged:: 3.3 | 
 | 106 |       the truncate method now accepts a ``size`` argument. | 
 | 107 |  | 
| Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 108 |  | 
| Martin Panter | 9b566c3 | 2015-11-07 00:32:50 +0000 | [diff] [blame] | 109 | .. function:: TemporaryDirectory(suffix=None, prefix=None, dir=None) | 
| Nick Coghlan | 543af75 | 2010-10-24 11:23:25 +0000 | [diff] [blame] | 110 |  | 
| Robert Collins | 2ebdc13 | 2015-08-13 11:38:02 +1200 | [diff] [blame] | 111 |    This function securely creates a temporary directory using the same rules as :func:`mkdtemp`. | 
| Nick Coghlan | 543af75 | 2010-10-24 11:23:25 +0000 | [diff] [blame] | 112 |    The resulting object can be used as a context manager (see | 
| Robert Collins | 2ebdc13 | 2015-08-13 11:38:02 +1200 | [diff] [blame] | 113 |    :ref:`tempfile-examples`).  On completion of the context or destruction | 
| R David Murray | 2368607 | 2014-02-05 14:53:40 -0500 | [diff] [blame] | 114 |    of the temporary directory object the newly created temporary directory | 
| Nick Coghlan | 543af75 | 2010-10-24 11:23:25 +0000 | [diff] [blame] | 115 |    and all its contents are removed from the filesystem. | 
 | 116 |  | 
| R David Murray | 2368607 | 2014-02-05 14:53:40 -0500 | [diff] [blame] | 117 |    The directory name can be retrieved from the :attr:`name` attribute of the | 
 | 118 |    returned object.  When the returned object is used as a context manager, the | 
 | 119 |    :attr:`name` will be assigned to the target of the :keyword:`as` clause in | 
 | 120 |    the :keyword:`with` statement, if there is one. | 
| Nick Coghlan | 543af75 | 2010-10-24 11:23:25 +0000 | [diff] [blame] | 121 |  | 
 | 122 |    The directory can be explicitly cleaned up by calling the | 
 | 123 |    :func:`cleanup` method. | 
 | 124 |  | 
 | 125 |    .. versionadded:: 3.2 | 
 | 126 |  | 
 | 127 |  | 
| Gregory P. Smith | ad577b9 | 2015-05-22 16:18:14 -0700 | [diff] [blame] | 128 | .. function:: mkstemp(suffix=None, prefix=None, dir=None, text=False) | 
| Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 129 |  | 
 | 130 |    Creates a temporary file in the most secure manner possible.  There are | 
 | 131 |    no race conditions in the file's creation, assuming that the platform | 
 | 132 |    properly implements the :const:`os.O_EXCL` flag for :func:`os.open`.  The | 
 | 133 |    file is readable and writable only by the creating user ID.  If the | 
 | 134 |    platform uses permission bits to indicate whether a file is executable, | 
 | 135 |    the file is executable by no one.  The file descriptor is not inherited | 
 | 136 |    by child processes. | 
 | 137 |  | 
 | 138 |    Unlike :func:`TemporaryFile`, the user of :func:`mkstemp` is responsible | 
 | 139 |    for deleting the temporary file when done with it. | 
 | 140 |  | 
| Martin Panter | 9b566c3 | 2015-11-07 00:32:50 +0000 | [diff] [blame] | 141 |    If *suffix* is not ``None``, the file name will end with that suffix, | 
| Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 142 |    otherwise there will be no suffix.  :func:`mkstemp` does not put a dot | 
 | 143 |    between the file name and the suffix; if you need one, put it at the | 
 | 144 |    beginning of *suffix*. | 
 | 145 |  | 
| Martin Panter | 9b566c3 | 2015-11-07 00:32:50 +0000 | [diff] [blame] | 146 |    If *prefix* is not ``None``, the file name will begin with that prefix; | 
 | 147 |    otherwise, a default prefix is used.  The default is the return value of | 
 | 148 |    :func:`gettempprefix` or :func:`gettempprefixb`, as appropriate. | 
| Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 149 |  | 
| Martin Panter | 9b566c3 | 2015-11-07 00:32:50 +0000 | [diff] [blame] | 150 |    If *dir* is not ``None``, the file will be created in that directory; | 
| Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 151 |    otherwise, a default directory is used.  The default directory is chosen | 
 | 152 |    from a platform-dependent list, but the user of the application can | 
 | 153 |    control the directory location by setting the *TMPDIR*, *TEMP* or *TMP* | 
 | 154 |    environment variables.  There is thus no guarantee that the generated | 
 | 155 |    filename will have any nice properties, such as not requiring quoting | 
 | 156 |    when passed to external commands via ``os.popen()``. | 
 | 157 |  | 
| Martin Panter | 9b566c3 | 2015-11-07 00:32:50 +0000 | [diff] [blame] | 158 |    If any of *suffix*, *prefix*, and *dir* are not | 
 | 159 |    ``None``, they must be the same type. | 
| Gregory P. Smith | ad577b9 | 2015-05-22 16:18:14 -0700 | [diff] [blame] | 160 |    If they are bytes, the returned name will be bytes instead of str. | 
 | 161 |    If you want to force a bytes return value with otherwise default behavior, | 
 | 162 |    pass ``suffix=b''``. | 
 | 163 |  | 
| Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 164 |    If *text* is specified, it indicates whether to open the file in binary | 
 | 165 |    mode (the default) or text mode.  On some platforms, this makes no | 
 | 166 |    difference. | 
 | 167 |  | 
 | 168 |    :func:`mkstemp` returns a tuple containing an OS-level handle to an open | 
 | 169 |    file (as would be returned by :func:`os.open`) and the absolute pathname | 
 | 170 |    of that file, in that order. | 
 | 171 |  | 
| Gregory P. Smith | ad577b9 | 2015-05-22 16:18:14 -0700 | [diff] [blame] | 172 |    .. versionchanged:: 3.5 | 
 | 173 |       *suffix*, *prefix*, and *dir* may now be supplied in bytes in order to | 
 | 174 |       obtain a bytes return value.  Prior to this, only str was allowed. | 
 | 175 |       *suffix* and *prefix* now accept and default to ``None`` to cause | 
 | 176 |       an appropriate default value to be used. | 
| Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 177 |  | 
| Gregory P. Smith | ad577b9 | 2015-05-22 16:18:14 -0700 | [diff] [blame] | 178 |  | 
 | 179 | .. function:: mkdtemp(suffix=None, prefix=None, dir=None) | 
| Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 180 |  | 
 | 181 |    Creates a temporary directory in the most secure manner possible. There | 
 | 182 |    are no race conditions in the directory's creation.  The directory is | 
 | 183 |    readable, writable, and searchable only by the creating user ID. | 
 | 184 |  | 
 | 185 |    The user of :func:`mkdtemp` is responsible for deleting the temporary | 
 | 186 |    directory and its contents when done with it. | 
 | 187 |  | 
 | 188 |    The *prefix*, *suffix*, and *dir* arguments are the same as for | 
 | 189 |    :func:`mkstemp`. | 
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 190 |  | 
 | 191 |    :func:`mkdtemp` returns the absolute pathname of the new directory. | 
 | 192 |  | 
| Gregory P. Smith | ad577b9 | 2015-05-22 16:18:14 -0700 | [diff] [blame] | 193 |    .. versionchanged:: 3.5 | 
 | 194 |       *suffix*, *prefix*, and *dir* may now be supplied in bytes in order to | 
 | 195 |       obtain a bytes return value.  Prior to this, only str was allowed. | 
 | 196 |       *suffix* and *prefix* now accept and default to ``None`` to cause | 
 | 197 |       an appropriate default value to be used. | 
 | 198 |  | 
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 199 |  | 
| Robert Collins | 2ebdc13 | 2015-08-13 11:38:02 +1200 | [diff] [blame] | 200 | .. function:: gettempdir() | 
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 201 |  | 
| Robert Collins | 2ebdc13 | 2015-08-13 11:38:02 +1200 | [diff] [blame] | 202 |    Return the name of the directory used for temporary files. This | 
 | 203 |    defines the default value for the *dir* argument to all functions | 
 | 204 |    in this module. | 
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 205 |  | 
| Robert Collins | 2ebdc13 | 2015-08-13 11:38:02 +1200 | [diff] [blame] | 206 |    Python searches a standard list of directories to find one which | 
 | 207 |    the calling user can create files in.  The list is: | 
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 208 |  | 
 | 209 |    #. The directory named by the :envvar:`TMPDIR` environment variable. | 
 | 210 |  | 
 | 211 |    #. The directory named by the :envvar:`TEMP` environment variable. | 
 | 212 |  | 
 | 213 |    #. The directory named by the :envvar:`TMP` environment variable. | 
 | 214 |  | 
 | 215 |    #. A platform-specific location: | 
 | 216 |  | 
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 217 |       * On Windows, the directories :file:`C:\\TEMP`, :file:`C:\\TMP`, | 
 | 218 |         :file:`\\TEMP`, and :file:`\\TMP`, in that order. | 
 | 219 |  | 
 | 220 |       * On all other platforms, the directories :file:`/tmp`, :file:`/var/tmp`, and | 
 | 221 |         :file:`/usr/tmp`, in that order. | 
 | 222 |  | 
 | 223 |    #. As a last resort, the current working directory. | 
 | 224 |  | 
| Robert Collins | 2ebdc13 | 2015-08-13 11:38:02 +1200 | [diff] [blame] | 225 |    The result of this search is cached, see the description of | 
 | 226 |    :data:`tempdir` below. | 
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 227 |  | 
| Gregory P. Smith | ad577b9 | 2015-05-22 16:18:14 -0700 | [diff] [blame] | 228 | .. function:: gettempdirb() | 
 | 229 |  | 
 | 230 |    Same as :func:`gettempdir` but the return value is in bytes. | 
 | 231 |  | 
 | 232 |    .. versionadded:: 3.5 | 
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 233 |  | 
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 234 | .. function:: gettempprefix() | 
 | 235 |  | 
 | 236 |    Return the filename prefix used to create temporary files.  This does not | 
| Georg Brandl | 4b26ff8 | 2008-08-04 07:24:52 +0000 | [diff] [blame] | 237 |    contain the directory component. | 
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 238 |  | 
| Gregory P. Smith | ad577b9 | 2015-05-22 16:18:14 -0700 | [diff] [blame] | 239 | .. function:: gettempprefixb() | 
 | 240 |  | 
| Martin Panter | 9b566c3 | 2015-11-07 00:32:50 +0000 | [diff] [blame] | 241 |    Same as :func:`gettempprefix` but the return value is in bytes. | 
| Gregory P. Smith | ad577b9 | 2015-05-22 16:18:14 -0700 | [diff] [blame] | 242 |  | 
 | 243 |    .. versionadded:: 3.5 | 
 | 244 |  | 
| Robert Collins | 2ebdc13 | 2015-08-13 11:38:02 +1200 | [diff] [blame] | 245 | The module uses a global variable to store the name of the directory | 
 | 246 | used for temporary files returned by :func:`gettempdir`.  It can be | 
 | 247 | set directly to override the selection process, but this is discouraged. | 
 | 248 | All functions in this module take a *dir* argument which can be used | 
 | 249 | to specify the directory and this is the recommend approach. | 
 | 250 |  | 
 | 251 | .. data:: tempdir | 
 | 252 |  | 
 | 253 |    When set to a value other than ``None``, this variable defines the | 
 | 254 |    default value for the *dir* argument to all the functions defined in this | 
 | 255 |    module. | 
 | 256 |  | 
 | 257 |    If ``tempdir`` is unset or ``None`` at any call to any of the above | 
 | 258 |    functions except :func:`gettempprefix` it is initalized following the | 
 | 259 |    algorithm described in :func:`gettempdir`. | 
 | 260 |  | 
 | 261 | .. _tempfile-examples: | 
| Nick Coghlan | 543af75 | 2010-10-24 11:23:25 +0000 | [diff] [blame] | 262 |  | 
 | 263 | Examples | 
 | 264 | -------- | 
 | 265 |  | 
 | 266 | Here are some examples of typical usage of the :mod:`tempfile` module:: | 
 | 267 |  | 
 | 268 |     >>> import tempfile | 
 | 269 |  | 
 | 270 |     # create a temporary file and write some data to it | 
 | 271 |     >>> fp = tempfile.TemporaryFile() | 
| Ross Lagerwall | 810b94a | 2011-04-10 09:30:04 +0200 | [diff] [blame] | 272 |     >>> fp.write(b'Hello world!') | 
| Nick Coghlan | 543af75 | 2010-10-24 11:23:25 +0000 | [diff] [blame] | 273 |     # read data from file | 
 | 274 |     >>> fp.seek(0) | 
 | 275 |     >>> fp.read() | 
| Ross Lagerwall | 810b94a | 2011-04-10 09:30:04 +0200 | [diff] [blame] | 276 |     b'Hello world!' | 
| Nick Coghlan | 543af75 | 2010-10-24 11:23:25 +0000 | [diff] [blame] | 277 |     # close the file, it will be removed | 
 | 278 |     >>> fp.close() | 
 | 279 |  | 
 | 280 |     # create a temporary file using a context manager | 
 | 281 |     >>> with tempfile.TemporaryFile() as fp: | 
| Ross Lagerwall | 810b94a | 2011-04-10 09:30:04 +0200 | [diff] [blame] | 282 |     ...     fp.write(b'Hello world!') | 
| Nick Coghlan | 543af75 | 2010-10-24 11:23:25 +0000 | [diff] [blame] | 283 |     ...     fp.seek(0) | 
 | 284 |     ...     fp.read() | 
| Ross Lagerwall | 810b94a | 2011-04-10 09:30:04 +0200 | [diff] [blame] | 285 |     b'Hello world!' | 
| Nick Coghlan | 543af75 | 2010-10-24 11:23:25 +0000 | [diff] [blame] | 286 |     >>> | 
 | 287 |     # file is now closed and removed | 
 | 288 |  | 
 | 289 |     # create a temporary directory using the context manager | 
 | 290 |     >>> with tempfile.TemporaryDirectory() as tmpdirname: | 
| Ross Lagerwall | 810b94a | 2011-04-10 09:30:04 +0200 | [diff] [blame] | 291 |     ...     print('created temporary directory', tmpdirname) | 
| Nick Coghlan | 543af75 | 2010-10-24 11:23:25 +0000 | [diff] [blame] | 292 |     >>> | 
 | 293 |     # directory and contents have been removed | 
 | 294 |  | 
| Robert Collins | 2ebdc13 | 2015-08-13 11:38:02 +1200 | [diff] [blame] | 295 |  | 
 | 296 | Deprecated functions and variables | 
 | 297 | ---------------------------------- | 
 | 298 |  | 
 | 299 | A historical way to create temporary files was to first generate a | 
 | 300 | file name with the :func:`mktemp` function and then create a file | 
 | 301 | using this name. Unfortunately this is not secure, because a different | 
 | 302 | process may create a file with this name in the time between the call | 
 | 303 | to :func:`mktemp` and the subsequent attempt to create the file by the | 
 | 304 | first process. The solution is to combine the two steps and create the | 
 | 305 | file immediately. This approach is used by :func:`mkstemp` and the | 
 | 306 | other functions described above. | 
 | 307 |  | 
 | 308 | .. function:: mktemp(suffix='', prefix='tmp', dir=None) | 
 | 309 |  | 
 | 310 |    .. deprecated:: 2.3 | 
 | 311 |       Use :func:`mkstemp` instead. | 
 | 312 |  | 
 | 313 |    Return an absolute pathname of a file that did not exist at the time the | 
| Martin Panter | 9b566c3 | 2015-11-07 00:32:50 +0000 | [diff] [blame] | 314 |    call is made.  The *prefix*, *suffix*, and *dir* arguments are similar | 
 | 315 |    to those of :func:`mkstemp`, except that bytes file names, ``suffix=None`` | 
 | 316 |    and ``prefix=None`` are not supported. | 
| Robert Collins | 2ebdc13 | 2015-08-13 11:38:02 +1200 | [diff] [blame] | 317 |  | 
 | 318 |    .. warning:: | 
 | 319 |  | 
 | 320 |       Use of this function may introduce a security hole in your program.  By | 
 | 321 |       the time you get around to doing anything with the file name it returns, | 
 | 322 |       someone else may have beaten you to the punch.  :func:`mktemp` usage can | 
 | 323 |       be replaced easily with :func:`NamedTemporaryFile`, passing it the | 
 | 324 |       ``delete=False`` parameter:: | 
 | 325 |  | 
 | 326 |          >>> f = NamedTemporaryFile(delete=False) | 
 | 327 |          >>> f.name | 
 | 328 |          '/tmp/tmptjujjt' | 
 | 329 |          >>> f.write(b"Hello World!\n") | 
 | 330 |          13 | 
 | 331 |          >>> f.close() | 
 | 332 |          >>> os.unlink(f.name) | 
 | 333 |          >>> os.path.exists(f.name) | 
 | 334 |          False |