Fred Drake | 3a0351c | 1998-04-04 07:23:21 +0000 | [diff] [blame] | 1 | \section{Standard Module \module{tempfile}} |
Guido van Rossum | e47da0a | 1997-07-17 16:34:52 +0000 | [diff] [blame] | 2 | \label{module-tempfile} |
Guido van Rossum | 81b3060 | 1995-03-01 14:36:00 +0000 | [diff] [blame] | 3 | \stmodindex{tempfile} |
| 4 | \indexii{temporary}{file name} |
| 5 | \indexii{temporary}{file} |
| 6 | |
Guido van Rossum | 81b3060 | 1995-03-01 14:36:00 +0000 | [diff] [blame] | 7 | |
Guido van Rossum | 6bb1adc | 1995-03-13 10:03:32 +0000 | [diff] [blame] | 8 | This module generates temporary file names. It is not \UNIX{} specific, |
| 9 | but it may require some help on non-\UNIX{} systems. |
Guido van Rossum | 81b3060 | 1995-03-01 14:36:00 +0000 | [diff] [blame] | 10 | |
| 11 | Note: the modules does not create temporary files, nor does it |
| 12 | automatically remove them when the current process exits or dies. |
| 13 | |
| 14 | The module defines a single user-callable function: |
| 15 | |
| 16 | \begin{funcdesc}{mktemp}{} |
| 17 | Return a unique temporary filename. This is an absolute pathname of a |
| 18 | file that does not exist at the time the call is made. No two calls |
| 19 | will return the same filename. |
| 20 | \end{funcdesc} |
| 21 | |
| 22 | The module uses two global variables that tell it how to construct a |
| 23 | temporary name. The caller may assign values to them; by default they |
Fred Drake | 92f31f1 | 1998-03-10 03:38:31 +0000 | [diff] [blame] | 24 | are initialized at the first call to \function{mktemp()}. |
Guido van Rossum | 81b3060 | 1995-03-01 14:36:00 +0000 | [diff] [blame] | 25 | |
| 26 | \begin{datadesc}{tempdir} |
| 27 | When set to a value other than \code{None}, this variable defines the |
Fred Drake | 92f31f1 | 1998-03-10 03:38:31 +0000 | [diff] [blame] | 28 | directory in which filenames returned by \function{mktemp()} reside. |
Fred Drake | 4f51558 | 1998-04-17 14:51:04 +0000 | [diff] [blame^] | 29 | The default is taken from the environment variable \envvar{TMPDIR}; if |
Fred Drake | 92f31f1 | 1998-03-10 03:38:31 +0000 | [diff] [blame] | 30 | this is not set, either \file{/usr/tmp} is used (on \UNIX{}), or the |
| 31 | current working directory (all other systems). No check is made to |
| 32 | see whether its value is valid. |
Guido van Rossum | 81b3060 | 1995-03-01 14:36:00 +0000 | [diff] [blame] | 33 | \end{datadesc} |
Guido van Rossum | 81b3060 | 1995-03-01 14:36:00 +0000 | [diff] [blame] | 34 | |
| 35 | \begin{datadesc}{template} |
| 36 | When set to a value other than \code{None}, this variable defines the |
| 37 | prefix of the final component of the filenames returned by |
Fred Drake | 92f31f1 | 1998-03-10 03:38:31 +0000 | [diff] [blame] | 38 | \function{mktemp()}. A string of decimal digits is added to generate |
Fred Drake | 12d9da5 | 1998-01-09 22:30:32 +0000 | [diff] [blame] | 39 | unique filenames. The default is either \file{@\var{pid}.} where |
| 40 | \var{pid} is the current process ID (on \UNIX{}), or \file{tmp} (all |
Guido van Rossum | 81b3060 | 1995-03-01 14:36:00 +0000 | [diff] [blame] | 41 | other systems). |
| 42 | \end{datadesc} |
| 43 | |
Fred Drake | 92f31f1 | 1998-03-10 03:38:31 +0000 | [diff] [blame] | 44 | \strong{Warning:} if a \UNIX{} process uses \code{mktemp()}, then |
| 45 | calls \function{fork()} and both parent and child continue to use |
| 46 | \function{mktemp()}, the processes will generate conflicting temporary |
Guido van Rossum | 81b3060 | 1995-03-01 14:36:00 +0000 | [diff] [blame] | 47 | names. To resolve this, the child process should assign \code{None} |
| 48 | to \code{template}, to force recomputing the default on the next call |
Fred Drake | 92f31f1 | 1998-03-10 03:38:31 +0000 | [diff] [blame] | 49 | to \function{mktemp()}. |