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