blob: 0a582e12dc58b39799cadffb9c3e0b8221ae9231 [file] [log] [blame]
Guido van Rossum470be141995-03-17 16:07:09 +00001\section{Standard Module \sectcode{tempfile}}
Guido van Rossum81b30601995-03-01 14:36:00 +00002\stmodindex{tempfile}
3\indexii{temporary}{file name}
4\indexii{temporary}{file}
5
6\renewcommand{\indexsubitem}{(in module tempfile)}
7
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
24are initialized at the first call to \code{mktemp()}.
25
26\begin{datadesc}{tempdir}
27When set to a value other than \code{None}, this variable defines the
28directory in which filenames returned by \code{mktemp()} reside. The
29default is taken from the environment variable \code{TMPDIR}; if this
Guido van Rossum6bb1adc1995-03-13 10:03:32 +000030is not set, either \code{/usr/tmp} is used (on \UNIX{}), or the current
Guido van Rossum81b30601995-03-01 14:36:00 +000031working directory (all other systems). No check is made to see
32whether its value is valid.
33\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
39\code{mktemp()}. A string of decimal digits is added to generate
40unique filenames. The default is either ``\code{@\var{pid}.}'' where
Guido van Rossum6bb1adc1995-03-13 10:03:32 +000041\var{pid} is the current process ID (on \UNIX{}), or ``\code{tmp}'' (all
Guido van Rossum81b30601995-03-01 14:36:00 +000042other systems).
43\end{datadesc}
44
Guido van Rossum6bb1adc1995-03-13 10:03:32 +000045Warning: if a \UNIX{} process uses \code{mktemp()}, then calls
Guido van Rossum81b30601995-03-01 14:36:00 +000046\code{fork()} and both parent and child continue to use
47\code{mktemp()}, the processes will generate conflicting temporary
48names. To resolve this, the child process should assign \code{None}
49to \code{template}, to force recomputing the default on the next call
50to \code{mktemp()}.