blob: 6ed763e6139bb1f1e075004327a4f52f946c650e [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
Fred Drake02330752000-05-26 19:05:16 +000014The module defines the following user-callable functions:
Guido van Rossum81b30601995-03-01 14:36:00 +000015
Fred Drake02330752000-05-26 19:05:16 +000016\begin{funcdesc}{mktemp}{\optional{suffix}}
Guido van Rossum81b30601995-03-01 14:36:00 +000017Return 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
Fred Drake02330752000-05-26 19:05:16 +000019will return the same filename. \var{suffix}, if provided, is used as
20the last part of the generated file name. This can be used to provide
21a filename extension or other identifying information that may be
22useful on some platforms.
23\end{funcdesc}
24
25\begin{funcdesc}{TemporaryFile}{\optional{mode\optional{,
26 bufsize\optional{, suffix}}}}
27Return a file (or file-like) object that can be used as a temporary
28storage area. The file is created in the most secure manner available
Thomas Woutersf8316632000-07-16 19:01:10 +000029in the appropriate temporary directory for the host platform. Under
Fred Drake02330752000-05-26 19:05:16 +000030\UNIX, the directory entry to the file is removed so that it is secure
31against attacks which involve creating symbolic links to the file or
32replacing the file with a symbolic link to some other file. For other
33platforms, which don't allow removing the directory entry while the
34file is in use, the file is automatically deleted as soon as it is
35closed (including an implicit close when it is garbage-collected).
36
37The \var{mode} parameter defaults to \code{'w+b'} so that the file
38created can be read and written without being closed. Binary mode is
39used so that it behaves consistently on all platforms without regard
40for the data that is stored. \var{bufsize} defaults to \code{-1},
41meaning that the operating system default is used. \var{suffix} is
42passed to \function{mktemp()}.
Guido van Rossum81b30601995-03-01 14:36:00 +000043\end{funcdesc}
44
45The module uses two global variables that tell it how to construct a
46temporary name. The caller may assign values to them; by default they
Fred Drake92f31f11998-03-10 03:38:31 +000047are initialized at the first call to \function{mktemp()}.
Guido van Rossum81b30601995-03-01 14:36:00 +000048
49\begin{datadesc}{tempdir}
50When set to a value other than \code{None}, this variable defines the
Fred Drake92f31f11998-03-10 03:38:31 +000051directory in which filenames returned by \function{mktemp()} reside.
Fred Drake4f515581998-04-17 14:51:04 +000052The default is taken from the environment variable \envvar{TMPDIR}; if
Fred Drakec37b65e2001-11-28 07:26:15 +000053this is not set, either \file{/usr/tmp} is used (on \UNIX), or the
Fred Drake92f31f11998-03-10 03:38:31 +000054current working directory (all other systems). No check is made to
55see whether its value is valid.
Guido van Rossum81b30601995-03-01 14:36:00 +000056\end{datadesc}
Guido van Rossum81b30601995-03-01 14:36:00 +000057
Fred Drakeb80a7772000-05-26 19:32:14 +000058\begin{funcdesc}{gettempprefix}{}
59Return the filename prefix used to create temporary files. This does
60not contain the directory component. Using this function is preferred
61over using the \code{template} variable directly.
62\versionadded{1.5.2}
63\end{funcdesc}
64
Guido van Rossum81b30601995-03-01 14:36:00 +000065\begin{datadesc}{template}
Fred Drake30f76ff2000-06-30 16:06:19 +000066\deprecated{2.0}{Use \function{gettempprefix()} instead.}
Guido van Rossum81b30601995-03-01 14:36:00 +000067When set to a value other than \code{None}, this variable defines the
68prefix of the final component of the filenames returned by
Fred Drake92f31f11998-03-10 03:38:31 +000069\function{mktemp()}. A string of decimal digits is added to generate
Fred Drake12d9da51998-01-09 22:30:32 +000070unique filenames. The default is either \file{@\var{pid}.} where
Fred Drakec37b65e2001-11-28 07:26:15 +000071\var{pid} is the current process ID (on \UNIX),
Fred Drakeb80a7772000-05-26 19:32:14 +000072\file{\textasciitilde\var{pid}-} on Windows NT, \file{Python-Tmp-} on
73MacOS, or \file{tmp} (all other systems).
74
75Older versions of this module used to require that \code{template} be
76set to \code{None} after a call to \function{os.fork()}; this has not
77been necessary since version 1.5.2.
Guido van Rossum81b30601995-03-01 14:36:00 +000078\end{datadesc}