cpython/Doc/lib/libtempfile.tex

50 lines
2.0 KiB
TeX
Raw Normal View History

\section{Standard Module \module{tempfile}}
\label{module-tempfile}
1995-03-01 14:36:00 +00:00
\stmodindex{tempfile}
\indexii{temporary}{file name}
\indexii{temporary}{file}
1995-03-13 10:03:32 +00:00
This module generates temporary file names. It is not \UNIX{} specific,
but it may require some help on non-\UNIX{} systems.
1995-03-01 14:36:00 +00:00
Note: the modules does not create temporary files, nor does it
automatically remove them when the current process exits or dies.
The module defines a single user-callable function:
\begin{funcdesc}{mktemp}{}
Return a unique temporary filename. This is an absolute pathname of a
file that does not exist at the time the call is made. No two calls
will return the same filename.
\end{funcdesc}
The module uses two global variables that tell it how to construct a
temporary name. The caller may assign values to them; by default they
1998-03-10 03:38:31 +00:00
are initialized at the first call to \function{mktemp()}.
1995-03-01 14:36:00 +00:00
\begin{datadesc}{tempdir}
When set to a value other than \code{None}, this variable defines the
1998-03-10 03:38:31 +00:00
directory in which filenames returned by \function{mktemp()} reside.
1998-04-17 14:51:04 +00:00
The default is taken from the environment variable \envvar{TMPDIR}; if
1998-03-10 03:38:31 +00:00
this is not set, either \file{/usr/tmp} is used (on \UNIX{}), or the
current working directory (all other systems). No check is made to
see whether its value is valid.
1995-03-01 14:36:00 +00:00
\end{datadesc}
\begin{datadesc}{template}
When set to a value other than \code{None}, this variable defines the
prefix of the final component of the filenames returned by
1998-03-10 03:38:31 +00:00
\function{mktemp()}. A string of decimal digits is added to generate
unique filenames. The default is either \file{@\var{pid}.} where
\var{pid} is the current process ID (on \UNIX{}), or \file{tmp} (all
1995-03-01 14:36:00 +00:00
other systems).
\end{datadesc}
1998-03-10 03:38:31 +00:00
\strong{Warning:} if a \UNIX{} process uses \code{mktemp()}, then
calls \function{fork()} and both parent and child continue to use
\function{mktemp()}, the processes will generate conflicting temporary
1995-03-01 14:36:00 +00:00
names. To resolve this, the child process should assign \code{None}
to \code{template}, to force recomputing the default on the next call
1998-03-10 03:38:31 +00:00
to \function{mktemp()}.