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