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