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