cpython/Doc/lib/libtempfile.tex

79 lines
3.5 KiB
TeX
Raw Normal View History

\section{\module{tempfile} ---
Generate temporary file names}
\declaremodule{standard}{tempfile}
\modulesynopsis{Generate temporary file names.}
1995-03-01 10:36:00 -04:00
\indexii{temporary}{file name}
\indexii{temporary}{file}
1995-03-13 06:03:32 -04: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 10:36:00 -04:00
The module defines the following user-callable functions:
1995-03-01 10:36:00 -04:00
\begin{funcdesc}{mktemp}{\optional{suffix}}
1995-03-01 10:36:00 -04:00
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. \var{suffix}, if provided, is used as
the last part of the generated file name. This can be used to provide
a filename extension or other identifying information that may be
useful on some platforms.
\end{funcdesc}
\begin{funcdesc}{TemporaryFile}{\optional{mode\optional{,
bufsize\optional{, suffix}}}}
Return a file (or file-like) object that can be used as a temporary
storage area. The file is created in the most secure manner available
in the appporpriate temporary directory for the host platform. Under
\UNIX, the directory entry to the file is removed so that it is secure
against attacks which involve creating symbolic links to the file or
replacing the file with a symbolic link to some other file. For other
platforms, which don't allow removing the directory entry while the
file is in use, the file is automatically deleted as soon as it is
closed (including an implicit close when it is garbage-collected).
The \var{mode} parameter defaults to \code{'w+b'} so that the file
created can be read and written without being closed. Binary mode is
used so that it behaves consistently on all platforms without regard
for the data that is stored. \var{bufsize} defaults to \code{-1},
meaning that the operating system default is used. \var{suffix} is
passed to \function{mktemp()}.
1995-03-01 10:36:00 -04:00
\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-09 23:38:31 -04:00
are initialized at the first call to \function{mktemp()}.
1995-03-01 10:36:00 -04:00
\begin{datadesc}{tempdir}
When set to a value other than \code{None}, this variable defines the
1998-03-09 23:38:31 -04:00
directory in which filenames returned by \function{mktemp()} reside.
1998-04-17 11:51:04 -03:00
The default is taken from the environment variable \envvar{TMPDIR}; if
1998-03-09 23:38:31 -04: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 10:36:00 -04:00
\end{datadesc}
\begin{funcdesc}{gettempprefix}{}
Return the filename prefix used to create temporary files. This does
not contain the directory component. Using this function is preferred
over using the \code{template} variable directly.
\versionadded{1.5.2}
\end{funcdesc}
1995-03-01 10:36:00 -04:00
\begin{datadesc}{template}
\deprecated{1.6}{Use \function{gettempprefix()} instead.}
1995-03-01 10:36:00 -04:00
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-09 23:38:31 -04: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{}),
\file{\textasciitilde\var{pid}-} on Windows NT, \file{Python-Tmp-} on
MacOS, or \file{tmp} (all other systems).
Older versions of this module used to require that \code{template} be
set to \code{None} after a call to \function{os.fork()}; this has not
been necessary since version 1.5.2.
1995-03-01 10:36:00 -04:00
\end{datadesc}