xref: /third_party/python/Doc/library/tempfile.rst (revision 7db96d56)
17db96d56Sopenharmony_ci:mod:`tempfile` --- Generate temporary files and directories
27db96d56Sopenharmony_ci============================================================
37db96d56Sopenharmony_ci
47db96d56Sopenharmony_ci.. module:: tempfile
57db96d56Sopenharmony_ci   :synopsis: Generate temporary files and directories.
67db96d56Sopenharmony_ci
77db96d56Sopenharmony_ci.. sectionauthor:: Zack Weinberg <zack@codesourcery.com>
87db96d56Sopenharmony_ci
97db96d56Sopenharmony_ci**Source code:** :source:`Lib/tempfile.py`
107db96d56Sopenharmony_ci
117db96d56Sopenharmony_ci.. index::
127db96d56Sopenharmony_ci   pair: temporary; file name
137db96d56Sopenharmony_ci   pair: temporary; file
147db96d56Sopenharmony_ci
157db96d56Sopenharmony_ci--------------
167db96d56Sopenharmony_ci
177db96d56Sopenharmony_ciThis module creates temporary files and directories.  It works on all
187db96d56Sopenharmony_cisupported platforms. :class:`TemporaryFile`, :class:`NamedTemporaryFile`,
197db96d56Sopenharmony_ci:class:`TemporaryDirectory`, and :class:`SpooledTemporaryFile` are high-level
207db96d56Sopenharmony_ciinterfaces which provide automatic cleanup and can be used as
217db96d56Sopenharmony_cicontext managers. :func:`mkstemp` and
227db96d56Sopenharmony_ci:func:`mkdtemp` are lower-level functions which require manual cleanup.
237db96d56Sopenharmony_ci
247db96d56Sopenharmony_ciAll the user-callable functions and constructors take additional arguments which
257db96d56Sopenharmony_ciallow direct control over the location and name of temporary files and
267db96d56Sopenharmony_cidirectories. Files names used by this module include a string of
277db96d56Sopenharmony_cirandom characters which allows those files to be securely created in
287db96d56Sopenharmony_cishared temporary directories.
297db96d56Sopenharmony_ciTo maintain backward compatibility, the argument order is somewhat odd; it
307db96d56Sopenharmony_ciis recommended to use keyword arguments for clarity.
317db96d56Sopenharmony_ci
327db96d56Sopenharmony_ciThe module defines the following user-callable items:
337db96d56Sopenharmony_ci
347db96d56Sopenharmony_ci.. function:: TemporaryFile(mode='w+b', buffering=-1, encoding=None, newline=None, suffix=None, prefix=None, dir=None, *, errors=None)
357db96d56Sopenharmony_ci
367db96d56Sopenharmony_ci   Return a :term:`file-like object` that can be used as a temporary storage area.
377db96d56Sopenharmony_ci   The file is created securely, using the same rules as :func:`mkstemp`. It will be destroyed as soon
387db96d56Sopenharmony_ci   as it is closed (including an implicit close when the object is garbage
397db96d56Sopenharmony_ci   collected).  Under Unix, the directory entry for the file is either not created at all or is removed
407db96d56Sopenharmony_ci   immediately after the file is created.  Other platforms do not support
417db96d56Sopenharmony_ci   this; your code should not rely on a temporary file created using this
427db96d56Sopenharmony_ci   function having or not having a visible name in the file system.
437db96d56Sopenharmony_ci
447db96d56Sopenharmony_ci   The resulting object can be used as a context manager (see
457db96d56Sopenharmony_ci   :ref:`tempfile-examples`).  On completion of the context or
467db96d56Sopenharmony_ci   destruction of the file object the temporary file will be removed
477db96d56Sopenharmony_ci   from the filesystem.
487db96d56Sopenharmony_ci
497db96d56Sopenharmony_ci   The *mode* parameter defaults to ``'w+b'`` so that the file created can
507db96d56Sopenharmony_ci   be read and written without being closed.  Binary mode is used so that it
517db96d56Sopenharmony_ci   behaves consistently on all platforms without regard for the data that is
527db96d56Sopenharmony_ci   stored.  *buffering*, *encoding*, *errors* and *newline* are interpreted as for
537db96d56Sopenharmony_ci   :func:`open`.
547db96d56Sopenharmony_ci
557db96d56Sopenharmony_ci   The *dir*, *prefix* and *suffix* parameters have the same meaning and
567db96d56Sopenharmony_ci   defaults as with :func:`mkstemp`.
577db96d56Sopenharmony_ci
587db96d56Sopenharmony_ci   The returned object is a true file object on POSIX platforms.  On other
597db96d56Sopenharmony_ci   platforms, it is a file-like object whose :attr:`!file` attribute is the
607db96d56Sopenharmony_ci   underlying true file object.
617db96d56Sopenharmony_ci
627db96d56Sopenharmony_ci   The :py:data:`os.O_TMPFILE` flag is used if it is available and works
637db96d56Sopenharmony_ci   (Linux-specific, requires Linux kernel 3.11 or later).
647db96d56Sopenharmony_ci
657db96d56Sopenharmony_ci   On platforms that are neither Posix nor Cygwin, TemporaryFile is an alias
667db96d56Sopenharmony_ci   for NamedTemporaryFile.
677db96d56Sopenharmony_ci
687db96d56Sopenharmony_ci   .. audit-event:: tempfile.mkstemp fullpath tempfile.TemporaryFile
697db96d56Sopenharmony_ci
707db96d56Sopenharmony_ci   .. versionchanged:: 3.5
717db96d56Sopenharmony_ci
727db96d56Sopenharmony_ci      The :py:data:`os.O_TMPFILE` flag is now used if available.
737db96d56Sopenharmony_ci
747db96d56Sopenharmony_ci   .. versionchanged:: 3.8
757db96d56Sopenharmony_ci      Added *errors* parameter.
767db96d56Sopenharmony_ci
777db96d56Sopenharmony_ci
787db96d56Sopenharmony_ci.. function:: NamedTemporaryFile(mode='w+b', buffering=-1, encoding=None, newline=None, suffix=None, prefix=None, dir=None, delete=True, *, errors=None)
797db96d56Sopenharmony_ci
807db96d56Sopenharmony_ci   This function operates exactly as :func:`TemporaryFile` does, except that
817db96d56Sopenharmony_ci   the file is guaranteed to have a visible name in the file system (on
827db96d56Sopenharmony_ci   Unix, the directory entry is not unlinked).  That name can be retrieved
837db96d56Sopenharmony_ci   from the :attr:`name` attribute of the returned
847db96d56Sopenharmony_ci   file-like object.  Whether the name can be
857db96d56Sopenharmony_ci   used to open the file a second time, while the named temporary file is
867db96d56Sopenharmony_ci   still open, varies across platforms (it can be so used on Unix; it cannot
877db96d56Sopenharmony_ci   on Windows).  If *delete* is true (the default), the file is
887db96d56Sopenharmony_ci   deleted as soon as it is closed.
897db96d56Sopenharmony_ci   The returned object is always a file-like object whose :attr:`!file`
907db96d56Sopenharmony_ci   attribute is the underlying true file object. This file-like object can
917db96d56Sopenharmony_ci   be used in a :keyword:`with` statement, just like a normal file.
927db96d56Sopenharmony_ci
937db96d56Sopenharmony_ci   On POSIX (only), a process that is terminated abruptly with SIGKILL
947db96d56Sopenharmony_ci   cannot automatically delete any NamedTemporaryFiles it created.
957db96d56Sopenharmony_ci
967db96d56Sopenharmony_ci   .. audit-event:: tempfile.mkstemp fullpath tempfile.NamedTemporaryFile
977db96d56Sopenharmony_ci
987db96d56Sopenharmony_ci   .. versionchanged:: 3.8
997db96d56Sopenharmony_ci      Added *errors* parameter.
1007db96d56Sopenharmony_ci
1017db96d56Sopenharmony_ci
1027db96d56Sopenharmony_ci.. class:: SpooledTemporaryFile(max_size=0, mode='w+b', buffering=-1, encoding=None, newline=None, suffix=None, prefix=None, dir=None, *, errors=None)
1037db96d56Sopenharmony_ci
1047db96d56Sopenharmony_ci   This class operates exactly as :func:`TemporaryFile` does, except that
1057db96d56Sopenharmony_ci   data is spooled in memory until the file size exceeds *max_size*, or
1067db96d56Sopenharmony_ci   until the file's :func:`fileno` method is called, at which point the
1077db96d56Sopenharmony_ci   contents are written to disk and operation proceeds as with
1087db96d56Sopenharmony_ci   :func:`TemporaryFile`.
1097db96d56Sopenharmony_ci
1107db96d56Sopenharmony_ci   The resulting file has one additional method, :func:`rollover`, which
1117db96d56Sopenharmony_ci   causes the file to roll over to an on-disk file regardless of its size.
1127db96d56Sopenharmony_ci
1137db96d56Sopenharmony_ci   The returned object is a file-like object whose :attr:`_file` attribute
1147db96d56Sopenharmony_ci   is either an :class:`io.BytesIO` or :class:`io.TextIOWrapper` object
1157db96d56Sopenharmony_ci   (depending on whether binary or text *mode* was specified) or a true file
1167db96d56Sopenharmony_ci   object, depending on whether :func:`rollover` has been called.  This
1177db96d56Sopenharmony_ci   file-like object can be used in a :keyword:`with` statement, just like
1187db96d56Sopenharmony_ci   a normal file.
1197db96d56Sopenharmony_ci
1207db96d56Sopenharmony_ci   .. versionchanged:: 3.3
1217db96d56Sopenharmony_ci      the truncate method now accepts a ``size`` argument.
1227db96d56Sopenharmony_ci
1237db96d56Sopenharmony_ci   .. versionchanged:: 3.8
1247db96d56Sopenharmony_ci      Added *errors* parameter.
1257db96d56Sopenharmony_ci
1267db96d56Sopenharmony_ci   .. versionchanged:: 3.11
1277db96d56Sopenharmony_ci      Fully implements the :class:`io.BufferedIOBase` and
1287db96d56Sopenharmony_ci      :class:`io.TextIOBase` abstract base classes (depending on whether binary
1297db96d56Sopenharmony_ci      or text *mode* was specified).
1307db96d56Sopenharmony_ci
1317db96d56Sopenharmony_ci
1327db96d56Sopenharmony_ci.. class:: TemporaryDirectory(suffix=None, prefix=None, dir=None, ignore_cleanup_errors=False)
1337db96d56Sopenharmony_ci
1347db96d56Sopenharmony_ci   This class securely creates a temporary directory using the same rules as :func:`mkdtemp`.
1357db96d56Sopenharmony_ci   The resulting object can be used as a context manager (see
1367db96d56Sopenharmony_ci   :ref:`tempfile-examples`).  On completion of the context or destruction
1377db96d56Sopenharmony_ci   of the temporary directory object, the newly created temporary directory
1387db96d56Sopenharmony_ci   and all its contents are removed from the filesystem.
1397db96d56Sopenharmony_ci
1407db96d56Sopenharmony_ci   The directory name can be retrieved from the :attr:`name` attribute of the
1417db96d56Sopenharmony_ci   returned object.  When the returned object is used as a context manager, the
1427db96d56Sopenharmony_ci   :attr:`name` will be assigned to the target of the :keyword:`!as` clause in
1437db96d56Sopenharmony_ci   the :keyword:`with` statement, if there is one.
1447db96d56Sopenharmony_ci
1457db96d56Sopenharmony_ci   The directory can be explicitly cleaned up by calling the
1467db96d56Sopenharmony_ci   :func:`cleanup` method. If *ignore_cleanup_errors* is true, any unhandled
1477db96d56Sopenharmony_ci   exceptions during explicit or implicit cleanup (such as a
1487db96d56Sopenharmony_ci   :exc:`PermissionError` removing open files on Windows) will be ignored,
1497db96d56Sopenharmony_ci   and the remaining removable items deleted on a "best-effort" basis.
1507db96d56Sopenharmony_ci   Otherwise, errors will be raised in whatever context cleanup occurs
1517db96d56Sopenharmony_ci   (the :func:`cleanup` call, exiting the context manager, when the object
1527db96d56Sopenharmony_ci   is garbage-collected or during interpreter shutdown).
1537db96d56Sopenharmony_ci
1547db96d56Sopenharmony_ci   .. audit-event:: tempfile.mkdtemp fullpath tempfile.TemporaryDirectory
1557db96d56Sopenharmony_ci
1567db96d56Sopenharmony_ci   .. versionadded:: 3.2
1577db96d56Sopenharmony_ci
1587db96d56Sopenharmony_ci   .. versionchanged:: 3.10
1597db96d56Sopenharmony_ci      Added *ignore_cleanup_errors* parameter.
1607db96d56Sopenharmony_ci
1617db96d56Sopenharmony_ci
1627db96d56Sopenharmony_ci.. function:: mkstemp(suffix=None, prefix=None, dir=None, text=False)
1637db96d56Sopenharmony_ci
1647db96d56Sopenharmony_ci   Creates a temporary file in the most secure manner possible.  There are
1657db96d56Sopenharmony_ci   no race conditions in the file's creation, assuming that the platform
1667db96d56Sopenharmony_ci   properly implements the :const:`os.O_EXCL` flag for :func:`os.open`.  The
1677db96d56Sopenharmony_ci   file is readable and writable only by the creating user ID.  If the
1687db96d56Sopenharmony_ci   platform uses permission bits to indicate whether a file is executable,
1697db96d56Sopenharmony_ci   the file is executable by no one.  The file descriptor is not inherited
1707db96d56Sopenharmony_ci   by child processes.
1717db96d56Sopenharmony_ci
1727db96d56Sopenharmony_ci   Unlike :func:`TemporaryFile`, the user of :func:`mkstemp` is responsible
1737db96d56Sopenharmony_ci   for deleting the temporary file when done with it.
1747db96d56Sopenharmony_ci
1757db96d56Sopenharmony_ci   If *suffix* is not ``None``, the file name will end with that suffix,
1767db96d56Sopenharmony_ci   otherwise there will be no suffix.  :func:`mkstemp` does not put a dot
1777db96d56Sopenharmony_ci   between the file name and the suffix; if you need one, put it at the
1787db96d56Sopenharmony_ci   beginning of *suffix*.
1797db96d56Sopenharmony_ci
1807db96d56Sopenharmony_ci   If *prefix* is not ``None``, the file name will begin with that prefix;
1817db96d56Sopenharmony_ci   otherwise, a default prefix is used.  The default is the return value of
1827db96d56Sopenharmony_ci   :func:`gettempprefix` or :func:`gettempprefixb`, as appropriate.
1837db96d56Sopenharmony_ci
1847db96d56Sopenharmony_ci   If *dir* is not ``None``, the file will be created in that directory;
1857db96d56Sopenharmony_ci   otherwise, a default directory is used.  The default directory is chosen
1867db96d56Sopenharmony_ci   from a platform-dependent list, but the user of the application can
1877db96d56Sopenharmony_ci   control the directory location by setting the *TMPDIR*, *TEMP* or *TMP*
1887db96d56Sopenharmony_ci   environment variables.  There is thus no guarantee that the generated
1897db96d56Sopenharmony_ci   filename will have any nice properties, such as not requiring quoting
1907db96d56Sopenharmony_ci   when passed to external commands via ``os.popen()``.
1917db96d56Sopenharmony_ci
1927db96d56Sopenharmony_ci   If any of *suffix*, *prefix*, and *dir* are not
1937db96d56Sopenharmony_ci   ``None``, they must be the same type.
1947db96d56Sopenharmony_ci   If they are bytes, the returned name will be bytes instead of str.
1957db96d56Sopenharmony_ci   If you want to force a bytes return value with otherwise default behavior,
1967db96d56Sopenharmony_ci   pass ``suffix=b''``.
1977db96d56Sopenharmony_ci
1987db96d56Sopenharmony_ci   If *text* is specified and true, the file is opened in text mode.
1997db96d56Sopenharmony_ci   Otherwise, (the default) the file is opened in binary mode.
2007db96d56Sopenharmony_ci
2017db96d56Sopenharmony_ci   :func:`mkstemp` returns a tuple containing an OS-level handle to an open
2027db96d56Sopenharmony_ci   file (as would be returned by :func:`os.open`) and the absolute pathname
2037db96d56Sopenharmony_ci   of that file, in that order.
2047db96d56Sopenharmony_ci
2057db96d56Sopenharmony_ci   .. audit-event:: tempfile.mkstemp fullpath tempfile.mkstemp
2067db96d56Sopenharmony_ci
2077db96d56Sopenharmony_ci   .. versionchanged:: 3.5
2087db96d56Sopenharmony_ci      *suffix*, *prefix*, and *dir* may now be supplied in bytes in order to
2097db96d56Sopenharmony_ci      obtain a bytes return value.  Prior to this, only str was allowed.
2107db96d56Sopenharmony_ci      *suffix* and *prefix* now accept and default to ``None`` to cause
2117db96d56Sopenharmony_ci      an appropriate default value to be used.
2127db96d56Sopenharmony_ci
2137db96d56Sopenharmony_ci   .. versionchanged:: 3.6
2147db96d56Sopenharmony_ci      The *dir* parameter now accepts a :term:`path-like object`.
2157db96d56Sopenharmony_ci
2167db96d56Sopenharmony_ci
2177db96d56Sopenharmony_ci.. function:: mkdtemp(suffix=None, prefix=None, dir=None)
2187db96d56Sopenharmony_ci
2197db96d56Sopenharmony_ci   Creates a temporary directory in the most secure manner possible. There
2207db96d56Sopenharmony_ci   are no race conditions in the directory's creation.  The directory is
2217db96d56Sopenharmony_ci   readable, writable, and searchable only by the creating user ID.
2227db96d56Sopenharmony_ci
2237db96d56Sopenharmony_ci   The user of :func:`mkdtemp` is responsible for deleting the temporary
2247db96d56Sopenharmony_ci   directory and its contents when done with it.
2257db96d56Sopenharmony_ci
2267db96d56Sopenharmony_ci   The *prefix*, *suffix*, and *dir* arguments are the same as for
2277db96d56Sopenharmony_ci   :func:`mkstemp`.
2287db96d56Sopenharmony_ci
2297db96d56Sopenharmony_ci   :func:`mkdtemp` returns the absolute pathname of the new directory if *dir*
2307db96d56Sopenharmony_ci   is ``None`` or is an absolute path. If *dir* is a relative path,
2317db96d56Sopenharmony_ci   :func:`mkdtemp` returns a relative path on Python 3.11 and lower. However,
2327db96d56Sopenharmony_ci   on 3.12 it will return an absolute path in all situations.
2337db96d56Sopenharmony_ci
2347db96d56Sopenharmony_ci   .. audit-event:: tempfile.mkdtemp fullpath tempfile.mkdtemp
2357db96d56Sopenharmony_ci
2367db96d56Sopenharmony_ci   .. versionchanged:: 3.5
2377db96d56Sopenharmony_ci      *suffix*, *prefix*, and *dir* may now be supplied in bytes in order to
2387db96d56Sopenharmony_ci      obtain a bytes return value.  Prior to this, only str was allowed.
2397db96d56Sopenharmony_ci      *suffix* and *prefix* now accept and default to ``None`` to cause
2407db96d56Sopenharmony_ci      an appropriate default value to be used.
2417db96d56Sopenharmony_ci
2427db96d56Sopenharmony_ci   .. versionchanged:: 3.6
2437db96d56Sopenharmony_ci      The *dir* parameter now accepts a :term:`path-like object`.
2447db96d56Sopenharmony_ci
2457db96d56Sopenharmony_ci
2467db96d56Sopenharmony_ci.. function:: gettempdir()
2477db96d56Sopenharmony_ci
2487db96d56Sopenharmony_ci   Return the name of the directory used for temporary files. This
2497db96d56Sopenharmony_ci   defines the default value for the *dir* argument to all functions
2507db96d56Sopenharmony_ci   in this module.
2517db96d56Sopenharmony_ci
2527db96d56Sopenharmony_ci   Python searches a standard list of directories to find one which
2537db96d56Sopenharmony_ci   the calling user can create files in.  The list is:
2547db96d56Sopenharmony_ci
2557db96d56Sopenharmony_ci   #. The directory named by the :envvar:`TMPDIR` environment variable.
2567db96d56Sopenharmony_ci
2577db96d56Sopenharmony_ci   #. The directory named by the :envvar:`TEMP` environment variable.
2587db96d56Sopenharmony_ci
2597db96d56Sopenharmony_ci   #. The directory named by the :envvar:`TMP` environment variable.
2607db96d56Sopenharmony_ci
2617db96d56Sopenharmony_ci   #. A platform-specific location:
2627db96d56Sopenharmony_ci
2637db96d56Sopenharmony_ci      * On Windows, the directories :file:`C:\\TEMP`, :file:`C:\\TMP`,
2647db96d56Sopenharmony_ci        :file:`\\TEMP`, and :file:`\\TMP`, in that order.
2657db96d56Sopenharmony_ci
2667db96d56Sopenharmony_ci      * On all other platforms, the directories :file:`/tmp`, :file:`/var/tmp`, and
2677db96d56Sopenharmony_ci        :file:`/usr/tmp`, in that order.
2687db96d56Sopenharmony_ci
2697db96d56Sopenharmony_ci   #. As a last resort, the current working directory.
2707db96d56Sopenharmony_ci
2717db96d56Sopenharmony_ci   The result of this search is cached, see the description of
2727db96d56Sopenharmony_ci   :data:`tempdir` below.
2737db96d56Sopenharmony_ci
2747db96d56Sopenharmony_ci   .. versionchanged:: 3.10
2757db96d56Sopenharmony_ci
2767db96d56Sopenharmony_ci      Always returns a str.  Previously it would return any :data:`tempdir`
2777db96d56Sopenharmony_ci      value regardless of type so long as it was not ``None``.
2787db96d56Sopenharmony_ci
2797db96d56Sopenharmony_ci.. function:: gettempdirb()
2807db96d56Sopenharmony_ci
2817db96d56Sopenharmony_ci   Same as :func:`gettempdir` but the return value is in bytes.
2827db96d56Sopenharmony_ci
2837db96d56Sopenharmony_ci   .. versionadded:: 3.5
2847db96d56Sopenharmony_ci
2857db96d56Sopenharmony_ci.. function:: gettempprefix()
2867db96d56Sopenharmony_ci
2877db96d56Sopenharmony_ci   Return the filename prefix used to create temporary files.  This does not
2887db96d56Sopenharmony_ci   contain the directory component.
2897db96d56Sopenharmony_ci
2907db96d56Sopenharmony_ci.. function:: gettempprefixb()
2917db96d56Sopenharmony_ci
2927db96d56Sopenharmony_ci   Same as :func:`gettempprefix` but the return value is in bytes.
2937db96d56Sopenharmony_ci
2947db96d56Sopenharmony_ci   .. versionadded:: 3.5
2957db96d56Sopenharmony_ci
2967db96d56Sopenharmony_ciThe module uses a global variable to store the name of the directory
2977db96d56Sopenharmony_ciused for temporary files returned by :func:`gettempdir`.  It can be
2987db96d56Sopenharmony_ciset directly to override the selection process, but this is discouraged.
2997db96d56Sopenharmony_ciAll functions in this module take a *dir* argument which can be used
3007db96d56Sopenharmony_cito specify the directory. This is the recommended approach that does
3017db96d56Sopenharmony_cinot surprise other unsuspecting code by changing global API behavior.
3027db96d56Sopenharmony_ci
3037db96d56Sopenharmony_ci.. data:: tempdir
3047db96d56Sopenharmony_ci
3057db96d56Sopenharmony_ci   When set to a value other than ``None``, this variable defines the
3067db96d56Sopenharmony_ci   default value for the *dir* argument to the functions defined in this
3077db96d56Sopenharmony_ci   module, including its type, bytes or str.  It cannot be a
3087db96d56Sopenharmony_ci   :term:`path-like object`.
3097db96d56Sopenharmony_ci
3107db96d56Sopenharmony_ci   If ``tempdir`` is ``None`` (the default) at any call to any of the above
3117db96d56Sopenharmony_ci   functions except :func:`gettempprefix` it is initialized following the
3127db96d56Sopenharmony_ci   algorithm described in :func:`gettempdir`.
3137db96d56Sopenharmony_ci
3147db96d56Sopenharmony_ci   .. note::
3157db96d56Sopenharmony_ci
3167db96d56Sopenharmony_ci      Beware that if you set ``tempdir`` to a bytes value, there is a
3177db96d56Sopenharmony_ci      nasty side effect: The global default return type of
3187db96d56Sopenharmony_ci      :func:`mkstemp` and :func:`mkdtemp` changes to bytes when no
3197db96d56Sopenharmony_ci      explicit ``prefix``, ``suffix``, or ``dir`` arguments of type
3207db96d56Sopenharmony_ci      str are supplied. Please do not write code expecting or
3217db96d56Sopenharmony_ci      depending on this. This awkward behavior is maintained for
3227db96d56Sopenharmony_ci      compatibility with the historical implementation.
3237db96d56Sopenharmony_ci
3247db96d56Sopenharmony_ci.. _tempfile-examples:
3257db96d56Sopenharmony_ci
3267db96d56Sopenharmony_ciExamples
3277db96d56Sopenharmony_ci--------
3287db96d56Sopenharmony_ci
3297db96d56Sopenharmony_ciHere are some examples of typical usage of the :mod:`tempfile` module::
3307db96d56Sopenharmony_ci
3317db96d56Sopenharmony_ci    >>> import tempfile
3327db96d56Sopenharmony_ci
3337db96d56Sopenharmony_ci    # create a temporary file and write some data to it
3347db96d56Sopenharmony_ci    >>> fp = tempfile.TemporaryFile()
3357db96d56Sopenharmony_ci    >>> fp.write(b'Hello world!')
3367db96d56Sopenharmony_ci    # read data from file
3377db96d56Sopenharmony_ci    >>> fp.seek(0)
3387db96d56Sopenharmony_ci    >>> fp.read()
3397db96d56Sopenharmony_ci    b'Hello world!'
3407db96d56Sopenharmony_ci    # close the file, it will be removed
3417db96d56Sopenharmony_ci    >>> fp.close()
3427db96d56Sopenharmony_ci
3437db96d56Sopenharmony_ci    # create a temporary file using a context manager
3447db96d56Sopenharmony_ci    >>> with tempfile.TemporaryFile() as fp:
3457db96d56Sopenharmony_ci    ...     fp.write(b'Hello world!')
3467db96d56Sopenharmony_ci    ...     fp.seek(0)
3477db96d56Sopenharmony_ci    ...     fp.read()
3487db96d56Sopenharmony_ci    b'Hello world!'
3497db96d56Sopenharmony_ci    >>>
3507db96d56Sopenharmony_ci    # file is now closed and removed
3517db96d56Sopenharmony_ci
3527db96d56Sopenharmony_ci    # create a temporary directory using the context manager
3537db96d56Sopenharmony_ci    >>> with tempfile.TemporaryDirectory() as tmpdirname:
3547db96d56Sopenharmony_ci    ...     print('created temporary directory', tmpdirname)
3557db96d56Sopenharmony_ci    >>>
3567db96d56Sopenharmony_ci    # directory and contents have been removed
3577db96d56Sopenharmony_ci
3587db96d56Sopenharmony_ci.. _tempfile-mktemp-deprecated:
3597db96d56Sopenharmony_ci
3607db96d56Sopenharmony_ciDeprecated functions and variables
3617db96d56Sopenharmony_ci----------------------------------
3627db96d56Sopenharmony_ci
3637db96d56Sopenharmony_ciA historical way to create temporary files was to first generate a
3647db96d56Sopenharmony_cifile name with the :func:`mktemp` function and then create a file
3657db96d56Sopenharmony_ciusing this name. Unfortunately this is not secure, because a different
3667db96d56Sopenharmony_ciprocess may create a file with this name in the time between the call
3677db96d56Sopenharmony_cito :func:`mktemp` and the subsequent attempt to create the file by the
3687db96d56Sopenharmony_cifirst process. The solution is to combine the two steps and create the
3697db96d56Sopenharmony_cifile immediately. This approach is used by :func:`mkstemp` and the
3707db96d56Sopenharmony_ciother functions described above.
3717db96d56Sopenharmony_ci
3727db96d56Sopenharmony_ci.. function:: mktemp(suffix='', prefix='tmp', dir=None)
3737db96d56Sopenharmony_ci
3747db96d56Sopenharmony_ci   .. deprecated:: 2.3
3757db96d56Sopenharmony_ci      Use :func:`mkstemp` instead.
3767db96d56Sopenharmony_ci
3777db96d56Sopenharmony_ci   Return an absolute pathname of a file that did not exist at the time the
3787db96d56Sopenharmony_ci   call is made.  The *prefix*, *suffix*, and *dir* arguments are similar
3797db96d56Sopenharmony_ci   to those of :func:`mkstemp`, except that bytes file names, ``suffix=None``
3807db96d56Sopenharmony_ci   and ``prefix=None`` are not supported.
3817db96d56Sopenharmony_ci
3827db96d56Sopenharmony_ci   .. warning::
3837db96d56Sopenharmony_ci
3847db96d56Sopenharmony_ci      Use of this function may introduce a security hole in your program.  By
3857db96d56Sopenharmony_ci      the time you get around to doing anything with the file name it returns,
3867db96d56Sopenharmony_ci      someone else may have beaten you to the punch.  :func:`mktemp` usage can
3877db96d56Sopenharmony_ci      be replaced easily with :func:`NamedTemporaryFile`, passing it the
3887db96d56Sopenharmony_ci      ``delete=False`` parameter::
3897db96d56Sopenharmony_ci
3907db96d56Sopenharmony_ci         >>> f = NamedTemporaryFile(delete=False)
3917db96d56Sopenharmony_ci         >>> f.name
3927db96d56Sopenharmony_ci         '/tmp/tmptjujjt'
3937db96d56Sopenharmony_ci         >>> f.write(b"Hello World!\n")
3947db96d56Sopenharmony_ci         13
3957db96d56Sopenharmony_ci         >>> f.close()
3967db96d56Sopenharmony_ci         >>> os.unlink(f.name)
3977db96d56Sopenharmony_ci         >>> os.path.exists(f.name)
3987db96d56Sopenharmony_ci         False
399