Fred Drake | 295da24 | 1998-08-10 19:42:37 +0000 | [diff] [blame] | 1 | \section{\module{tempfile} --- |
Guido van Rossum | 830a515 | 2002-08-09 16:16:30 +0000 | [diff] [blame] | 2 | Generate temporary files and directories} |
| 3 | \sectionauthor{Zack Weinberg}{zack@codesourcery.com} |
Fred Drake | b91e934 | 1998-07-23 17:59:49 +0000 | [diff] [blame] | 4 | |
Fred Drake | 737aa55 | 1999-04-21 17:01:15 +0000 | [diff] [blame] | 5 | \declaremodule{standard}{tempfile} |
Guido van Rossum | 830a515 | 2002-08-09 16:16:30 +0000 | [diff] [blame] | 6 | \modulesynopsis{Generate temporary files and directories.} |
Fred Drake | b91e934 | 1998-07-23 17:59:49 +0000 | [diff] [blame] | 7 | |
Guido van Rossum | 81b3060 | 1995-03-01 14:36:00 +0000 | [diff] [blame] | 8 | \indexii{temporary}{file name} |
| 9 | \indexii{temporary}{file} |
| 10 | |
Guido van Rossum | 830a515 | 2002-08-09 16:16:30 +0000 | [diff] [blame] | 11 | This module generates temporary files and directories. It works on |
| 12 | all supported platforms. |
Guido van Rossum | 81b3060 | 1995-03-01 14:36:00 +0000 | [diff] [blame] | 13 | |
Guido van Rossum | 830a515 | 2002-08-09 16:16:30 +0000 | [diff] [blame] | 14 | In version 2.3 of Python, this module was overhauled for enhanced |
| 15 | security. It now provides three new functions, |
Fred Drake | 7099583 | 2003-04-22 18:54:53 +0000 | [diff] [blame] | 16 | \function{NamedTemporaryFile()}, \function{mkstemp()}, and |
| 17 | \function{mkdtemp()}, which should eliminate all remaining need to use |
| 18 | the insecure \function{mktemp()} function. Temporary file names created |
Guido van Rossum | 830a515 | 2002-08-09 16:16:30 +0000 | [diff] [blame] | 19 | by this module no longer contain the process ID; instead a string of |
| 20 | six random characters is used. |
| 21 | |
| 22 | Also, all the user-callable functions now take additional arguments |
| 23 | which allow direct control over the location and name of temporary |
| 24 | files. It is no longer necessary to use the global \var{tempdir} and |
| 25 | \var{template} variables. To maintain backward compatibility, the |
| 26 | argument order is somewhat odd; it is recommended to use keyword |
| 27 | arguments for clarity. |
Guido van Rossum | 81b3060 | 1995-03-01 14:36:00 +0000 | [diff] [blame] | 28 | |
Fred Drake | 0233075 | 2000-05-26 19:05:16 +0000 | [diff] [blame] | 29 | The module defines the following user-callable functions: |
Guido van Rossum | 81b3060 | 1995-03-01 14:36:00 +0000 | [diff] [blame] | 30 | |
Fred Drake | 614438a | 2003-09-11 18:18:54 +0000 | [diff] [blame] | 31 | \begin{funcdesc}{TemporaryFile}{\optional{mode=\code{'w+b'}\optional{, |
| 32 | bufsize=\code{-1}\optional{, |
| 33 | suffix\optional{, prefix\optional{, dir}}}}}} |
Fred Drake | 0233075 | 2000-05-26 19:05:16 +0000 | [diff] [blame] | 34 | Return a file (or file-like) object that can be used as a temporary |
Guido van Rossum | 830a515 | 2002-08-09 16:16:30 +0000 | [diff] [blame] | 35 | storage area. The file is created using \function{mkstemp}. It will |
| 36 | be destroyed as soon as it is closed (including an implicit close when |
| 37 | the object is garbage collected). Under \UNIX, the directory entry |
| 38 | for the file is removed immediately after the file is created. Other |
| 39 | platforms do not support this; your code should not rely on a |
Fred Drake | 7099583 | 2003-04-22 18:54:53 +0000 | [diff] [blame] | 40 | temporary file created using this function having or not having a |
| 41 | visible name in the file system. |
Fred Drake | 0233075 | 2000-05-26 19:05:16 +0000 | [diff] [blame] | 42 | |
| 43 | The \var{mode} parameter defaults to \code{'w+b'} so that the file |
| 44 | created can be read and written without being closed. Binary mode is |
| 45 | used so that it behaves consistently on all platforms without regard |
| 46 | for the data that is stored. \var{bufsize} defaults to \code{-1}, |
Guido van Rossum | 830a515 | 2002-08-09 16:16:30 +0000 | [diff] [blame] | 47 | meaning that the operating system default is used. |
| 48 | |
| 49 | The \var{dir}, \var{prefix} and \var{suffix} parameters are passed to |
Fred Drake | 7099583 | 2003-04-22 18:54:53 +0000 | [diff] [blame] | 50 | \function{mkstemp()}. |
Guido van Rossum | 830a515 | 2002-08-09 16:16:30 +0000 | [diff] [blame] | 51 | \end{funcdesc} |
| 52 | |
Fred Drake | 614438a | 2003-09-11 18:18:54 +0000 | [diff] [blame] | 53 | \begin{funcdesc}{NamedTemporaryFile}{\optional{mode=\code{'w+b'}\optional{, |
| 54 | bufsize=\code{-1}\optional{, |
| 55 | suffix\optional{, prefix\optional{, |
| 56 | dir}}}}}} |
Fred Drake | 7099583 | 2003-04-22 18:54:53 +0000 | [diff] [blame] | 57 | This function operates exactly as \function{TemporaryFile()} does, |
Guido van Rossum | 830a515 | 2002-08-09 16:16:30 +0000 | [diff] [blame] | 58 | except that the file is guaranteed to have a visible name in the file |
Tim Peters | 3350b5b | 2002-11-21 16:32:11 +0000 | [diff] [blame] | 59 | system (on \UNIX, the directory entry is not unlinked). That name can |
| 60 | be retrieved from the \member{name} member of the file object. Whether |
| 61 | the name can be used to open the file a second time, while the |
| 62 | named temporary file is still open, varies across platforms (it can |
| 63 | be so used on \UNIX; it cannot on Windows NT or later). |
Guido van Rossum | 830a515 | 2002-08-09 16:16:30 +0000 | [diff] [blame] | 64 | \versionadded{2.3} |
| 65 | \end{funcdesc} |
| 66 | |
Fred Drake | 614438a | 2003-09-11 18:18:54 +0000 | [diff] [blame] | 67 | \begin{funcdesc}{mkstemp}{\optional{suffix\optional{, |
| 68 | prefix\optional{, dir\optional{, text}}}}} |
Guido van Rossum | 830a515 | 2002-08-09 16:16:30 +0000 | [diff] [blame] | 69 | Creates a temporary file in the most secure manner possible. There |
| 70 | are no race conditions in the file's creation, assuming that the |
| 71 | platform properly implements the \constant{O_EXCL} flag for |
Fred Drake | 7099583 | 2003-04-22 18:54:53 +0000 | [diff] [blame] | 72 | \function{os.open()}. The file is readable and writable only by the |
Guido van Rossum | 830a515 | 2002-08-09 16:16:30 +0000 | [diff] [blame] | 73 | creating user ID. If the platform uses permission bits to indicate |
| 74 | whether a file is executable, the file is executable by no one. The |
| 75 | file descriptor is not inherited by child processes. |
| 76 | |
Fred Drake | 7099583 | 2003-04-22 18:54:53 +0000 | [diff] [blame] | 77 | Unlike \function{TemporaryFile()}, the user of \function{mkstemp()} is |
Guido van Rossum | 830a515 | 2002-08-09 16:16:30 +0000 | [diff] [blame] | 78 | responsible for deleting the temporary file when done with it. |
| 79 | |
| 80 | If \var{suffix} is specified, the file name will end with that suffix, |
Fred Drake | 7099583 | 2003-04-22 18:54:53 +0000 | [diff] [blame] | 81 | otherwise there will be no suffix. \function{mkstemp()} does not put a |
Guido van Rossum | 830a515 | 2002-08-09 16:16:30 +0000 | [diff] [blame] | 82 | dot between the file name and the suffix; if you need one, put it at |
| 83 | the beginning of \var{suffix}. |
| 84 | |
| 85 | If \var{prefix} is specified, the file name will begin with that |
| 86 | prefix; otherwise, a default prefix is used. |
| 87 | |
| 88 | If \var{dir} is specified, the file will be created in that directory; |
| 89 | otherwise, a default directory is used. |
| 90 | |
Tim Peters | 2f238c1 | 2002-08-14 16:37:10 +0000 | [diff] [blame] | 91 | If \var{text} is specified, it indicates whether to open the file in |
Guido van Rossum | 830a515 | 2002-08-09 16:16:30 +0000 | [diff] [blame] | 92 | binary mode (the default) or text mode. On some platforms, this makes |
| 93 | no difference. |
| 94 | |
Fred Drake | 7099583 | 2003-04-22 18:54:53 +0000 | [diff] [blame] | 95 | \function{mkstemp()} returns a tuple containing an OS-level handle to |
| 96 | an open file (as would be returned by \function{os.open()}) and the |
Guido van Rossum | 830a515 | 2002-08-09 16:16:30 +0000 | [diff] [blame] | 97 | absolute pathname of that file, in that order. |
| 98 | \versionadded{2.3} |
| 99 | \end{funcdesc} |
| 100 | |
Fred Drake | 614438a | 2003-09-11 18:18:54 +0000 | [diff] [blame] | 101 | \begin{funcdesc}{mkdtemp}{\optional{suffix\optional{, prefix\optional{, dir}}}} |
Guido van Rossum | 830a515 | 2002-08-09 16:16:30 +0000 | [diff] [blame] | 102 | Creates a temporary directory in the most secure manner possible. |
| 103 | There are no race conditions in the directory's creation. The |
| 104 | directory is readable, writable, and searchable only by the |
| 105 | creating user ID. |
| 106 | |
Fred Drake | 7099583 | 2003-04-22 18:54:53 +0000 | [diff] [blame] | 107 | The user of \function{mkdtemp()} is responsible for deleting the |
Guido van Rossum | 830a515 | 2002-08-09 16:16:30 +0000 | [diff] [blame] | 108 | temporary directory and its contents when done with it. |
| 109 | |
| 110 | The \var{prefix}, \var{suffix}, and \var{dir} arguments are the same |
Fred Drake | 7099583 | 2003-04-22 18:54:53 +0000 | [diff] [blame] | 111 | as for \function{mkstemp()}. |
Guido van Rossum | 830a515 | 2002-08-09 16:16:30 +0000 | [diff] [blame] | 112 | |
Fred Drake | 7099583 | 2003-04-22 18:54:53 +0000 | [diff] [blame] | 113 | \function{mkdtemp()} returns the absolute pathname of the new directory. |
Guido van Rossum | 830a515 | 2002-08-09 16:16:30 +0000 | [diff] [blame] | 114 | \versionadded{2.3} |
| 115 | \end{funcdesc} |
| 116 | |
Fred Drake | 614438a | 2003-09-11 18:18:54 +0000 | [diff] [blame] | 117 | \begin{funcdesc}{mktemp}{\optional{suffix\optional{, prefix\optional{, dir}}}} |
Guido van Rossum | 830a515 | 2002-08-09 16:16:30 +0000 | [diff] [blame] | 118 | \deprecated{2.3}{Use \function{mkstemp()} instead.} |
| 119 | Return an absolute pathname of a file that did not exist at the time |
| 120 | the call is made. The \var{prefix}, \var{suffix}, and \var{dir} |
Fred Drake | 7099583 | 2003-04-22 18:54:53 +0000 | [diff] [blame] | 121 | arguments are the same as for \function{mkstemp()}. |
Guido van Rossum | 830a515 | 2002-08-09 16:16:30 +0000 | [diff] [blame] | 122 | |
| 123 | \warning{Use of this function may introduce a security hole in your |
| 124 | program. By the time you get around to doing anything with the file |
| 125 | name it returns, someone else may have beaten you to the punch.} |
Guido van Rossum | 81b3060 | 1995-03-01 14:36:00 +0000 | [diff] [blame] | 126 | \end{funcdesc} |
| 127 | |
| 128 | The module uses two global variables that tell it how to construct a |
Guido van Rossum | 830a515 | 2002-08-09 16:16:30 +0000 | [diff] [blame] | 129 | temporary name. They are initialized at the first call to any of the |
| 130 | functions above. The caller may change them, but this is discouraged; |
| 131 | use the appropriate function arguments, instead. |
Guido van Rossum | 81b3060 | 1995-03-01 14:36:00 +0000 | [diff] [blame] | 132 | |
| 133 | \begin{datadesc}{tempdir} |
| 134 | When set to a value other than \code{None}, this variable defines the |
Guido van Rossum | 830a515 | 2002-08-09 16:16:30 +0000 | [diff] [blame] | 135 | default value for the \var{dir} argument to all the functions defined |
| 136 | in this module. |
| 137 | |
Fred Drake | 614438a | 2003-09-11 18:18:54 +0000 | [diff] [blame] | 138 | If \code{tempdir} is unset or \code{None} at any call to any of the |
Guido van Rossum | 830a515 | 2002-08-09 16:16:30 +0000 | [diff] [blame] | 139 | above functions, Python searches a standard list of directories and |
| 140 | sets \var{tempdir} to the first one which the calling user can create |
| 141 | files in. The list is: |
| 142 | |
| 143 | \begin{enumerate} |
| 144 | \item The directory named by the \envvar{TMPDIR} environment variable. |
| 145 | \item The directory named by the \envvar{TEMP} environment variable. |
| 146 | \item The directory named by the \envvar{TMP} environment variable. |
| 147 | \item A platform-specific location: |
| 148 | \begin{itemize} |
Guido van Rossum | 830a515 | 2002-08-09 16:16:30 +0000 | [diff] [blame] | 149 | \item On RiscOS, the directory named by the |
| 150 | \envvar{Wimp\$ScrapDir} environment variable. |
| 151 | \item On Windows, the directories |
| 152 | \file{C:$\backslash$TEMP}, |
| 153 | \file{C:$\backslash$TMP}, |
| 154 | \file{$\backslash$TEMP}, and |
| 155 | \file{$\backslash$TMP}, in that order. |
| 156 | \item On all other platforms, the directories |
| 157 | \file{/tmp}, \file{/var/tmp}, and \file{/usr/tmp}, in that order. |
| 158 | \end{itemize} |
| 159 | \item As a last resort, the current working directory. |
| 160 | \end{enumerate} |
Guido van Rossum | 81b3060 | 1995-03-01 14:36:00 +0000 | [diff] [blame] | 161 | \end{datadesc} |
Guido van Rossum | 81b3060 | 1995-03-01 14:36:00 +0000 | [diff] [blame] | 162 | |
Guido van Rossum | 830a515 | 2002-08-09 16:16:30 +0000 | [diff] [blame] | 163 | \begin{funcdesc}{gettempdir}{} |
| 164 | Return the directory currently selected to create temporary files in. |
Fred Drake | 614438a | 2003-09-11 18:18:54 +0000 | [diff] [blame] | 165 | If \code{tempdir} is not \code{None}, this simply returns its contents; |
Guido van Rossum | 830a515 | 2002-08-09 16:16:30 +0000 | [diff] [blame] | 166 | otherwise, the search described above is performed, and the result |
| 167 | returned. |
Fred Drake | b80a777 | 2000-05-26 19:32:14 +0000 | [diff] [blame] | 168 | \end{funcdesc} |
| 169 | |
Guido van Rossum | 81b3060 | 1995-03-01 14:36:00 +0000 | [diff] [blame] | 170 | \begin{datadesc}{template} |
Fred Drake | 30f76ff | 2000-06-30 16:06:19 +0000 | [diff] [blame] | 171 | \deprecated{2.0}{Use \function{gettempprefix()} instead.} |
Guido van Rossum | 81b3060 | 1995-03-01 14:36:00 +0000 | [diff] [blame] | 172 | When set to a value other than \code{None}, this variable defines the |
| 173 | prefix of the final component of the filenames returned by |
Guido van Rossum | 830a515 | 2002-08-09 16:16:30 +0000 | [diff] [blame] | 174 | \function{mktemp()}. A string of six random letters and digits is |
| 175 | appended to the prefix to make the filename unique. On Windows, |
| 176 | the default prefix is \file{\textasciitilde{}T}; on all other systems |
| 177 | it is \file{tmp}. |
Fred Drake | b80a777 | 2000-05-26 19:32:14 +0000 | [diff] [blame] | 178 | |
| 179 | Older versions of this module used to require that \code{template} be |
| 180 | set to \code{None} after a call to \function{os.fork()}; this has not |
| 181 | been necessary since version 1.5.2. |
Guido van Rossum | 81b3060 | 1995-03-01 14:36:00 +0000 | [diff] [blame] | 182 | \end{datadesc} |
Guido van Rossum | 830a515 | 2002-08-09 16:16:30 +0000 | [diff] [blame] | 183 | |
| 184 | \begin{funcdesc}{gettempprefix}{} |
| 185 | Return the filename prefix used to create temporary files. This does |
| 186 | not contain the directory component. Using this function is preferred |
| 187 | over reading the \var{template} variable directly. |
| 188 | \versionadded{1.5.2} |
| 189 | \end{funcdesc} |