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