blob: 93577e760ff05e88aed6968fbb79896009754607 [file] [log] [blame]
Fred Drake3a0351c1998-04-04 07:23:21 +00001\section{Standard Module \module{tempfile}}
Guido van Rossume47da0a1997-07-17 16:34:52 +00002\label{module-tempfile}
Guido van Rossum81b30601995-03-01 14:36:00 +00003\stmodindex{tempfile}
4\indexii{temporary}{file name}
5\indexii{temporary}{file}
6
Guido van Rossum81b30601995-03-01 14:36:00 +00007
Guido van Rossum6bb1adc1995-03-13 10:03:32 +00008This module generates temporary file names. It is not \UNIX{} specific,
9but it may require some help on non-\UNIX{} systems.
Guido van Rossum81b30601995-03-01 14:36:00 +000010
11Note: the modules does not create temporary files, nor does it
12automatically remove them when the current process exits or dies.
13
14The module defines a single user-callable function:
15
16\begin{funcdesc}{mktemp}{}
17Return a unique temporary filename. This is an absolute pathname of a
18file that does not exist at the time the call is made. No two calls
19will return the same filename.
20\end{funcdesc}
21
22The module uses two global variables that tell it how to construct a
23temporary name. The caller may assign values to them; by default they
Fred Drake92f31f11998-03-10 03:38:31 +000024are initialized at the first call to \function{mktemp()}.
Guido van Rossum81b30601995-03-01 14:36:00 +000025
26\begin{datadesc}{tempdir}
27When set to a value other than \code{None}, this variable defines the
Fred Drake92f31f11998-03-10 03:38:31 +000028directory in which filenames returned by \function{mktemp()} reside.
Fred Drake4f515581998-04-17 14:51:04 +000029The default is taken from the environment variable \envvar{TMPDIR}; if
Fred Drake92f31f11998-03-10 03:38:31 +000030this is not set, either \file{/usr/tmp} is used (on \UNIX{}), or the
31current working directory (all other systems). No check is made to
32see whether its value is valid.
Guido van Rossum81b30601995-03-01 14:36:00 +000033\end{datadesc}
Guido van Rossum81b30601995-03-01 14:36:00 +000034
35\begin{datadesc}{template}
36When set to a value other than \code{None}, this variable defines the
37prefix of the final component of the filenames returned by
Fred Drake92f31f11998-03-10 03:38:31 +000038\function{mktemp()}. A string of decimal digits is added to generate
Fred Drake12d9da51998-01-09 22:30:32 +000039unique 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 Rossum81b30601995-03-01 14:36:00 +000041other systems).
42\end{datadesc}
43
Fred Drake92f31f11998-03-10 03:38:31 +000044\strong{Warning:} if a \UNIX{} process uses \code{mktemp()}, then
45calls \function{fork()} and both parent and child continue to use
46\function{mktemp()}, the processes will generate conflicting temporary
Guido van Rossum81b30601995-03-01 14:36:00 +000047names. To resolve this, the child process should assign \code{None}
48to \code{template}, to force recomputing the default on the next call
Fred Drake92f31f11998-03-10 03:38:31 +000049to \function{mktemp()}.