Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +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 | |
Éric Araujo | 29a0b57 | 2011-08-19 02:14:03 +0200 | [diff] [blame] | 15 | **Source code:** :source:`Lib/tempfile.py` |
| 16 | |
| 17 | -------------- |
| 18 | |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 19 | This module generates temporary files and directories. It works on all |
| 20 | supported platforms. |
| 21 | |
| 22 | In version 2.3 of Python, this module was overhauled for enhanced security. It |
| 23 | now provides three new functions, :func:`NamedTemporaryFile`, :func:`mkstemp`, |
| 24 | and :func:`mkdtemp`, which should eliminate all remaining need to use the |
| 25 | insecure :func:`mktemp` function. Temporary file names created by this module |
| 26 | no longer contain the process ID; instead a string of six random characters is |
| 27 | used. |
| 28 | |
Skip Montanaro | 7980992 | 2008-04-27 22:52:02 +0000 | [diff] [blame] | 29 | Also, all the user-callable functions now take additional arguments which |
| 30 | allow direct control over the location and name of temporary files. It is |
| 31 | no longer necessary to use the global *tempdir* and *template* variables. |
| 32 | To maintain backward compatibility, the argument order is somewhat odd; it |
| 33 | is recommended to use keyword arguments for clarity. |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 34 | |
| 35 | The module defines the following user-callable functions: |
| 36 | |
| 37 | |
Skip Montanaro | 7980992 | 2008-04-27 22:52:02 +0000 | [diff] [blame] | 38 | .. function:: TemporaryFile([mode='w+b'[, bufsize=-1[, suffix=''[, prefix='tmp'[, dir=None]]]]]) |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 39 | |
Skip Montanaro | 7980992 | 2008-04-27 22:52:02 +0000 | [diff] [blame] | 40 | Return a file-like object that can be used as a temporary storage area. |
| 41 | The file is created using :func:`mkstemp`. It will be destroyed as soon |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 42 | as it is closed (including an implicit close when the object is garbage |
Skip Montanaro | 7980992 | 2008-04-27 22:52:02 +0000 | [diff] [blame] | 43 | collected). Under Unix, the directory entry for the file is removed |
| 44 | immediately after the file is created. Other platforms do not support |
| 45 | this; your code should not rely on a temporary file created using this |
| 46 | function having or not having a visible name in the file system. |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 47 | |
Skip Montanaro | 7980992 | 2008-04-27 22:52:02 +0000 | [diff] [blame] | 48 | The *mode* parameter defaults to ``'w+b'`` so that the file created can |
| 49 | be read and written without being closed. Binary mode is used so that it |
| 50 | behaves consistently on all platforms without regard for the data that is |
| 51 | stored. *bufsize* defaults to ``-1``, meaning that the operating system |
| 52 | default is used. |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 53 | |
| 54 | The *dir*, *prefix* and *suffix* parameters are passed to :func:`mkstemp`. |
| 55 | |
Georg Brandl | c4768a4 | 2008-01-06 15:55:26 +0000 | [diff] [blame] | 56 | The returned object is a true file object on POSIX platforms. On other |
Georg Brandl | 9fa61bb | 2009-07-26 14:19:57 +0000 | [diff] [blame] | 57 | platforms, it is a file-like object whose :attr:`!file` attribute is the |
Skip Montanaro | 7980992 | 2008-04-27 22:52:02 +0000 | [diff] [blame] | 58 | underlying true file object. This file-like object can be used in a |
| 59 | :keyword:`with` statement, just like a normal file. |
Georg Brandl | c4768a4 | 2008-01-06 15:55:26 +0000 | [diff] [blame] | 60 | |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 61 | |
Skip Montanaro | 7980992 | 2008-04-27 22:52:02 +0000 | [diff] [blame] | 62 | .. function:: NamedTemporaryFile([mode='w+b'[, bufsize=-1[, suffix=''[, prefix='tmp'[, dir=None[, delete=True]]]]]]) |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 63 | |
Skip Montanaro | 7980992 | 2008-04-27 22:52:02 +0000 | [diff] [blame] | 64 | This function operates exactly as :func:`TemporaryFile` does, except that |
| 65 | the file is guaranteed to have a visible name in the file system (on |
| 66 | Unix, the directory entry is not unlinked). That name can be retrieved |
Senthil Kumaran | 6f18b98 | 2011-07-04 12:50:02 -0700 | [diff] [blame] | 67 | from the :attr:`name` attribute of the file object. Whether the name can be |
Skip Montanaro | 7980992 | 2008-04-27 22:52:02 +0000 | [diff] [blame] | 68 | used to open the file a second time, while the named temporary file is |
| 69 | still open, varies across platforms (it can be so used on Unix; it cannot |
| 70 | on Windows NT or later). If *delete* is true (the default), the file is |
| 71 | deleted as soon as it is closed. |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 72 | |
Georg Brandl | 9fa61bb | 2009-07-26 14:19:57 +0000 | [diff] [blame] | 73 | The returned object is always a file-like object whose :attr:`!file` |
Skip Montanaro | 7980992 | 2008-04-27 22:52:02 +0000 | [diff] [blame] | 74 | attribute is the underlying true file object. This file-like object can |
| 75 | be used in a :keyword:`with` statement, just like a normal file. |
Georg Brandl | c4768a4 | 2008-01-06 15:55:26 +0000 | [diff] [blame] | 76 | |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 77 | .. versionadded:: 2.3 |
| 78 | |
| 79 | .. versionadded:: 2.6 |
| 80 | The *delete* parameter. |
| 81 | |
| 82 | |
Skip Montanaro | 7980992 | 2008-04-27 22:52:02 +0000 | [diff] [blame] | 83 | .. function:: SpooledTemporaryFile([max_size=0, [mode='w+b'[, bufsize=-1[, suffix=''[, prefix='tmp'[, dir=None]]]]]]) |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 84 | |
Skip Montanaro | 7980992 | 2008-04-27 22:52:02 +0000 | [diff] [blame] | 85 | This function operates exactly as :func:`TemporaryFile` does, except that |
| 86 | data is spooled in memory until the file size exceeds *max_size*, or |
| 87 | until the file's :func:`fileno` method is called, at which point the |
| 88 | contents are written to disk and operation proceeds as with |
| 89 | :func:`TemporaryFile`. |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 90 | |
Skip Montanaro | 7980992 | 2008-04-27 22:52:02 +0000 | [diff] [blame] | 91 | The resulting file has one additional method, :func:`rollover`, which |
| 92 | causes the file to roll over to an on-disk file regardless of its size. |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 93 | |
Georg Brandl | c4768a4 | 2008-01-06 15:55:26 +0000 | [diff] [blame] | 94 | The returned object is a file-like object whose :attr:`_file` attribute |
| 95 | is either a :class:`StringIO` object or a true file object, depending on |
Skip Montanaro | 7980992 | 2008-04-27 22:52:02 +0000 | [diff] [blame] | 96 | whether :func:`rollover` has been called. This file-like object can be |
| 97 | used in a :keyword:`with` statement, just like a normal file. |
Georg Brandl | c4768a4 | 2008-01-06 15:55:26 +0000 | [diff] [blame] | 98 | |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 99 | .. versionadded:: 2.6 |
| 100 | |
| 101 | |
Skip Montanaro | 7980992 | 2008-04-27 22:52:02 +0000 | [diff] [blame] | 102 | .. function:: mkstemp([suffix=''[, prefix='tmp'[, dir=None[, text=False]]]]) |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 103 | |
Skip Montanaro | 7980992 | 2008-04-27 22:52:02 +0000 | [diff] [blame] | 104 | Creates a temporary file in the most secure manner possible. There are |
| 105 | no race conditions in the file's creation, assuming that the platform |
| 106 | properly implements the :const:`os.O_EXCL` flag for :func:`os.open`. The |
| 107 | file is readable and writable only by the creating user ID. If the |
| 108 | platform uses permission bits to indicate whether a file is executable, |
| 109 | the file is executable by no one. The file descriptor is not inherited |
| 110 | by child processes. |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 111 | |
Skip Montanaro | 7980992 | 2008-04-27 22:52:02 +0000 | [diff] [blame] | 112 | Unlike :func:`TemporaryFile`, the user of :func:`mkstemp` is responsible |
| 113 | for deleting the temporary file when done with it. |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 114 | |
Skip Montanaro | 7980992 | 2008-04-27 22:52:02 +0000 | [diff] [blame] | 115 | If *suffix* is specified, the file name will end with that suffix, |
| 116 | otherwise there will be no suffix. :func:`mkstemp` does not put a dot |
| 117 | between the file name and the suffix; if you need one, put it at the |
| 118 | beginning of *suffix*. |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 119 | |
Skip Montanaro | 7980992 | 2008-04-27 22:52:02 +0000 | [diff] [blame] | 120 | If *prefix* is specified, the file name will begin with that prefix; |
| 121 | otherwise, a default prefix is used. |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 122 | |
Skip Montanaro | 7980992 | 2008-04-27 22:52:02 +0000 | [diff] [blame] | 123 | If *dir* is specified, the file will be created in that directory; |
| 124 | otherwise, a default directory is used. The default directory is chosen |
| 125 | from a platform-dependent list, but the user of the application can |
| 126 | control the directory location by setting the *TMPDIR*, *TEMP* or *TMP* |
| 127 | environment variables. There is thus no guarantee that the generated |
| 128 | filename will have any nice properties, such as not requiring quoting |
| 129 | when passed to external commands via ``os.popen()``. |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 130 | |
Skip Montanaro | 7980992 | 2008-04-27 22:52:02 +0000 | [diff] [blame] | 131 | If *text* is specified, it indicates whether to open the file in binary |
| 132 | mode (the default) or text mode. On some platforms, this makes no |
| 133 | difference. |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 134 | |
Skip Montanaro | 7980992 | 2008-04-27 22:52:02 +0000 | [diff] [blame] | 135 | :func:`mkstemp` returns a tuple containing an OS-level handle to an open |
| 136 | file (as would be returned by :func:`os.open`) and the absolute pathname |
| 137 | of that file, in that order. |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 138 | |
| 139 | .. versionadded:: 2.3 |
| 140 | |
| 141 | |
Skip Montanaro | 7980992 | 2008-04-27 22:52:02 +0000 | [diff] [blame] | 142 | .. function:: mkdtemp([suffix=''[, prefix='tmp'[, dir=None]]]) |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 143 | |
Skip Montanaro | 7980992 | 2008-04-27 22:52:02 +0000 | [diff] [blame] | 144 | Creates a temporary directory in the most secure manner possible. There |
| 145 | are no race conditions in the directory's creation. The directory is |
| 146 | readable, writable, and searchable only by the creating user ID. |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 147 | |
Skip Montanaro | 7980992 | 2008-04-27 22:52:02 +0000 | [diff] [blame] | 148 | The user of :func:`mkdtemp` is responsible for deleting the temporary |
| 149 | directory and its contents when done with it. |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 150 | |
Skip Montanaro | 7980992 | 2008-04-27 22:52:02 +0000 | [diff] [blame] | 151 | The *prefix*, *suffix*, and *dir* arguments are the same as for |
| 152 | :func:`mkstemp`. |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 153 | |
| 154 | :func:`mkdtemp` returns the absolute pathname of the new directory. |
| 155 | |
| 156 | .. versionadded:: 2.3 |
| 157 | |
| 158 | |
Skip Montanaro | 7980992 | 2008-04-27 22:52:02 +0000 | [diff] [blame] | 159 | .. function:: mktemp([suffix=''[, prefix='tmp'[, dir=None]]]) |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 160 | |
| 161 | .. deprecated:: 2.3 |
| 162 | Use :func:`mkstemp` instead. |
| 163 | |
Skip Montanaro | 7980992 | 2008-04-27 22:52:02 +0000 | [diff] [blame] | 164 | Return an absolute pathname of a file that did not exist at the time the |
| 165 | call is made. The *prefix*, *suffix*, and *dir* arguments are the same |
| 166 | as for :func:`mkstemp`. |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 167 | |
| 168 | .. warning:: |
| 169 | |
Georg Brandl | e92818f | 2009-01-03 20:47:01 +0000 | [diff] [blame] | 170 | Use of this function may introduce a security hole in your program. By |
| 171 | the time you get around to doing anything with the file name it returns, |
| 172 | someone else may have beaten you to the punch. :func:`mktemp` usage can |
| 173 | be replaced easily with :func:`NamedTemporaryFile`, passing it the |
| 174 | ``delete=False`` parameter:: |
Skip Montanaro | e404a12 | 2008-05-09 00:45:00 +0000 | [diff] [blame] | 175 | |
Benjamin Peterson | d03238a | 2008-05-09 00:50:40 +0000 | [diff] [blame] | 176 | >>> f = NamedTemporaryFile(delete=False) |
Benjamin Peterson | d03238a | 2008-05-09 00:50:40 +0000 | [diff] [blame] | 177 | >>> f |
| 178 | <open file '<fdopen>', mode 'w+b' at 0x384698> |
| 179 | >>> f.name |
| 180 | '/var/folders/5q/5qTPn6xq2RaWqk+1Ytw3-U+++TI/-Tmp-/tmpG7V1Y0' |
| 181 | >>> f.write("Hello World!\n") |
| 182 | >>> f.close() |
| 183 | >>> os.unlink(f.name) |
| 184 | >>> os.path.exists(f.name) |
| 185 | False |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 186 | |
Skip Montanaro | 7980992 | 2008-04-27 22:52:02 +0000 | [diff] [blame] | 187 | The module uses two global variables that tell it how to construct a |
| 188 | temporary name. They are initialized at the first call to any of the |
| 189 | functions above. The caller may change them, but this is discouraged; use |
| 190 | the appropriate function arguments, instead. |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 191 | |
| 192 | |
| 193 | .. data:: tempdir |
| 194 | |
Skip Montanaro | 7980992 | 2008-04-27 22:52:02 +0000 | [diff] [blame] | 195 | When set to a value other than ``None``, this variable defines the |
| 196 | default value for the *dir* argument to all the functions defined in this |
| 197 | module. |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 198 | |
Skip Montanaro | 7980992 | 2008-04-27 22:52:02 +0000 | [diff] [blame] | 199 | If ``tempdir`` is unset or ``None`` at any call to any of the above |
| 200 | functions, Python searches a standard list of directories and sets |
| 201 | *tempdir* to the first one which the calling user can create files in. |
| 202 | The list is: |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 203 | |
| 204 | #. The directory named by the :envvar:`TMPDIR` environment variable. |
| 205 | |
| 206 | #. The directory named by the :envvar:`TEMP` environment variable. |
| 207 | |
| 208 | #. The directory named by the :envvar:`TMP` environment variable. |
| 209 | |
| 210 | #. A platform-specific location: |
| 211 | |
| 212 | * On RiscOS, the directory named by the :envvar:`Wimp$ScrapDir` environment |
| 213 | variable. |
| 214 | |
| 215 | * On Windows, the directories :file:`C:\\TEMP`, :file:`C:\\TMP`, |
| 216 | :file:`\\TEMP`, and :file:`\\TMP`, in that order. |
| 217 | |
| 218 | * On all other platforms, the directories :file:`/tmp`, :file:`/var/tmp`, and |
| 219 | :file:`/usr/tmp`, in that order. |
| 220 | |
| 221 | #. As a last resort, the current working directory. |
| 222 | |
| 223 | |
| 224 | .. function:: gettempdir() |
| 225 | |
| 226 | Return the directory currently selected to create temporary files in. If |
| 227 | :data:`tempdir` is not ``None``, this simply returns its contents; otherwise, |
| 228 | the search described above is performed, and the result returned. |
| 229 | |
Benjamin Peterson | b06b4c3 | 2008-10-29 20:33:00 +0000 | [diff] [blame] | 230 | .. versionadded:: 2.3 |
| 231 | |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 232 | |
| 233 | .. data:: template |
| 234 | |
| 235 | .. deprecated:: 2.0 |
| 236 | Use :func:`gettempprefix` instead. |
| 237 | |
| 238 | When set to a value other than ``None``, this variable defines the prefix of the |
| 239 | final component of the filenames returned by :func:`mktemp`. A string of six |
| 240 | random letters and digits is appended to the prefix to make the filename unique. |
Georg Brandl | 40df8ec | 2008-08-04 07:23:29 +0000 | [diff] [blame] | 241 | The default prefix is :file:`tmp`. |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 242 | |
| 243 | Older versions of this module used to require that ``template`` be set to |
| 244 | ``None`` after a call to :func:`os.fork`; this has not been necessary since |
| 245 | version 1.5.2. |
| 246 | |
| 247 | |
| 248 | .. function:: gettempprefix() |
| 249 | |
| 250 | Return the filename prefix used to create temporary files. This does not |
| 251 | contain the directory component. Using this function is preferred over reading |
| 252 | the *template* variable directly. |
| 253 | |
| 254 | .. versionadded:: 1.5.2 |
| 255 | |