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