Guido van Rossum | 470be14 | 1995-03-17 16:07:09 +0000 | [diff] [blame] | 1 | \section{Standard Module \sectcode{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 | |
| 7 | \renewcommand{\indexsubitem}{(in module tempfile)} |
| 8 | |
Guido van Rossum | 6bb1adc | 1995-03-13 10:03:32 +0000 | [diff] [blame] | 9 | This module generates temporary file names. It is not \UNIX{} specific, |
| 10 | but it may require some help on non-\UNIX{} systems. |
Guido van Rossum | 81b3060 | 1995-03-01 14:36:00 +0000 | [diff] [blame] | 11 | |
| 12 | Note: the modules does not create temporary files, nor does it |
| 13 | automatically remove them when the current process exits or dies. |
| 14 | |
| 15 | The module defines a single user-callable function: |
| 16 | |
| 17 | \begin{funcdesc}{mktemp}{} |
| 18 | Return a unique temporary filename. This is an absolute pathname of a |
| 19 | file that does not exist at the time the call is made. No two calls |
| 20 | will return the same filename. |
| 21 | \end{funcdesc} |
| 22 | |
| 23 | The module uses two global variables that tell it how to construct a |
| 24 | temporary name. The caller may assign values to them; by default they |
| 25 | are initialized at the first call to \code{mktemp()}. |
| 26 | |
| 27 | \begin{datadesc}{tempdir} |
| 28 | When set to a value other than \code{None}, this variable defines the |
| 29 | directory in which filenames returned by \code{mktemp()} reside. The |
| 30 | default is taken from the environment variable \code{TMPDIR}; if this |
Fred Drake | 12d9da5 | 1998-01-09 22:30:32 +0000 | [diff] [blame] | 31 | is not set, either \file{/usr/tmp} is used (on \UNIX{}), or the current |
Guido van Rossum | 81b3060 | 1995-03-01 14:36:00 +0000 | [diff] [blame] | 32 | working directory (all other systems). No check is made to see |
| 33 | whether its value is valid. |
| 34 | \end{datadesc} |
| 35 | \ttindex{TMPDIR} |
| 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 |
| 40 | \code{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 | |
Guido van Rossum | 6bb1adc | 1995-03-13 10:03:32 +0000 | [diff] [blame] | 46 | Warning: if a \UNIX{} process uses \code{mktemp()}, then calls |
Guido van Rossum | 81b3060 | 1995-03-01 14:36:00 +0000 | [diff] [blame] | 47 | \code{fork()} and both parent and child continue to use |
| 48 | \code{mktemp()}, the processes will generate conflicting temporary |
| 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 |
| 51 | to \code{mktemp()}. |