blob: d81084e11e11caec6ad7c4616a2d2fcc4ed6ca76 [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.
29The default is taken from the environment variable \code{TMPDIR}; if
30this 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}
34\ttindex{TMPDIR}
35
36\begin{datadesc}{template}
37When set to a value other than \code{None}, this variable defines the
38prefix of the final component of the filenames returned by
Fred Drake92f31f11998-03-10 03:38:31 +000039\function{mktemp()}. A string of decimal digits is added to generate
Fred Drake12d9da51998-01-09 22:30:32 +000040unique filenames. The default is either \file{@\var{pid}.} where
41\var{pid} is the current process ID (on \UNIX{}), or \file{tmp} (all
Guido van Rossum81b30601995-03-01 14:36:00 +000042other systems).
43\end{datadesc}
44
Fred Drake92f31f11998-03-10 03:38:31 +000045\strong{Warning:} if a \UNIX{} process uses \code{mktemp()}, then
46calls \function{fork()} and both parent and child continue to use
47\function{mktemp()}, the processes will generate conflicting temporary
Guido van Rossum81b30601995-03-01 14:36:00 +000048names. To resolve this, the child process should assign \code{None}
49to \code{template}, to force recomputing the default on the next call
Fred Drake92f31f11998-03-10 03:38:31 +000050to \function{mktemp()}.