# tempfile - pydoc - phpman

Help on module tempfile:

## NAME
    tempfile - Temporary files.

## MODULE REFERENCE
    <https://docs.python.org/3.10/library/tempfile.html>

    The following documentation is automatically generated from the Python
    source files.  It may be incomplete, incorrect or include features that
    are considered implementation detail and may vary between Python
    implementations.  When in doubt, consult the module reference at the
    location listed above.

## DESCRIPTION
    This module provides generic, low- and high-level interfaces for
    creating temporary files and directories.  All of the interfaces
    provided by this module can be used without fear of race conditions
    except for 'mktemp'.  'mktemp' is subject to race conditions and
    should not be used; it is provided for backward compatibility only.

    The default path names are returned as str.  If you supply bytes as
    input, all return values will be in bytes.  Ex:

        >>> tempfile.mkstemp()
        (4, '/tmp/tmptpu9nin8')
        >>> tempfile.mkdtemp(suffix=b'')
        b'/tmp/tmppbi8f0hy'

    This module also provides some data items to the user:

      TMP_MAX  - maximum number of names that will be tried before
                 giving up.
      tempdir  - If this is set to a string before the first use of
                 any routine from this module, it will be considered as
                 another candidate location to store temporary files.

## CLASSES
    builtins.object
        SpooledTemporaryFile
        TemporaryDirectory

### class SpooledTemporaryFile
     |  SpooledTemporaryFile(max_size=0, mode='w+b', buffering=-1, encoding=None, newline=None, suffix=None, prefix=None, dir=None, *, errors=None)
     |
     |  Temporary file wrapper, specialized to switch from BytesIO
     |  or StringIO to a real file when it exceeds a certain size or
     |  when a fileno is needed.
     |
     |  Methods defined here:
     |
     |  __enter__(self)
     |      # Context management protocol
     |
     |  __exit__(self, exc, value, tb)
     |
     |  __init__(self, max_size=0, mode='w+b', buffering=-1, encoding=None, newline=None, suffix=None, prefix=None, dir=None, *, errors=None)
     |      Initialize self.  See help(type(self)) for accurate signature.
     |
     |  __iter__(self)
     |      # file protocol
     |
     |  close(self)
     |
     |  fileno(self)
     |
     |  flush(self)
     |
     |  isatty(self)
     |
     |  read(self, *args)
     |
     |  readline(self, *args)
     |
     |  readlines(self, *args)
     |
     |  rollover(self)
     |
     |  seek(self, *args)
     |
     |  tell(self)
     |
     |  truncate(self, size=None)
     |
     |  write(self, s)
     |
     |  writelines(self, iterable)
     |
     |  ----------------------------------------------------------------------
     |  Class methods defined here:
     |
     |  __class_getitem__ = GenericAlias(...) from builtins.type
     |      Represent a PEP 585 generic type
     |
     |      E.g. for t = list[int], t.__origin__ is list and t.__args__ is (int,).
     |
     |  ----------------------------------------------------------------------
     |  Readonly properties defined here:
     |
     |  closed
     |
     |  encoding
     |
     |  errors
     |
     |  mode
     |
     |  name
     |
     |  newlines
     |
     |  ----------------------------------------------------------------------
     |  Data descriptors defined here:
     |
     |  __dict__
     |      dictionary for instance variables (if defined)
     |
     |  __weakref__
     |      list of weak references to the object (if defined)

### class TemporaryDirectory
     |  TemporaryDirectory(suffix=None, prefix=None, dir=None, ignore_cleanup_errors=False)
     |
     |  Create and return a temporary directory.  This has the same
     |  behavior as mkdtemp but can be used as a context manager.  For
     |  example:
     |
     |      with TemporaryDirectory() as tmpdir:
     |          ...
     |
     |  Upon exiting the context, the directory and everything contained
     |  in it are removed.
     |
     |  Methods defined here:
     |
     |  __enter__(self)
     |
     |  __exit__(self, exc, value, tb)
     |
     |  __init__(self, suffix=None, prefix=None, dir=None, ignore_cleanup_errors=False)
     |      Initialize self.  See help(type(self)) for accurate signature.
     |
     |  __repr__(self)
     |      Return repr(self).
     |
     |  cleanup(self)
     |
     |  ----------------------------------------------------------------------
     |  Class methods defined here:
     |
     |  __class_getitem__ = GenericAlias(...) from builtins.type
     |      Represent a PEP 585 generic type
     |
     |      E.g. for t = list[int], t.__origin__ is list and t.__args__ is (int,).
     |
     |  ----------------------------------------------------------------------
     |  Data descriptors defined here:
     |
     |  __dict__
     |      dictionary for instance variables (if defined)
     |
     |  __weakref__
     |      list of weak references to the object (if defined)

## FUNCTIONS
    NamedTemporaryFile(mode='w+b', buffering=-1, encoding=None, newline=None, suffix=None, prefix=None, dir=None, delete=True, *, errors=None)
        Create and return a temporary file.
        Arguments:
        'prefix', 'suffix', 'dir' -- as for mkstemp.
        'mode' -- the mode argument to io.open (default "w+b").
        'buffering' -- the buffer size argument to io.open (default -1).
        'encoding' -- the encoding argument to io.open (default None)
        'newline' -- the newline argument to io.open (default None)
        'delete' -- whether the file is deleted on close (default True).
        'errors' -- the errors argument to io.open (default None)
        The file is created as mkstemp() would do it.

        Returns an object with a file-like interface; the name of the file
        is accessible as its 'name' attribute.  The file will be automatically
        deleted when it is closed unless the 'delete' argument is set to False.

    TemporaryFile(mode='w+b', buffering=-1, encoding=None, newline=None, suffix=None, prefix=None, dir=None, *, errors=None)
        Create and return a temporary file.
        Arguments:
        'prefix', 'suffix', 'dir' -- as for mkstemp.
        'mode' -- the mode argument to io.open (default "w+b").
        'buffering' -- the buffer size argument to io.open (default -1).
        'encoding' -- the encoding argument to io.open (default None)
        'newline' -- the newline argument to io.open (default None)
        'errors' -- the errors argument to io.open (default None)
        The file is created as mkstemp() would do it.

        Returns an object with a file-like interface.  The file has no
        name, and will cease to exist when it is closed.

### gettempdir
        Returns tempfile.tempdir as str.

### gettempdirb
        Returns tempfile.tempdir as bytes.

### gettempprefix
        The default prefix for temporary directories as string.

### gettempprefixb
        The default prefix for temporary directories as bytes.

### mkdtemp
        User-callable function to create and return a unique temporary
        directory.  The return value is the pathname of the directory.

        Arguments are as for mkstemp, except that the 'text' argument is
        not accepted.

        The directory is readable, writable, and searchable only by the
        creating user.

        Caller is responsible for deleting the directory when done with it.

### mkstemp
        User-callable function to create and return a unique temporary
        file.  The return value is a pair (fd, name) where fd is the
        file descriptor returned by os.open, and name is the filename.

        If 'suffix' is not None, the file name will end with that suffix,
        otherwise there will be no suffix.

        If 'prefix' is not None, the file name will begin with that prefix,
        otherwise a default prefix is used.

        If 'dir' is not None, the file will be created in that directory,
        otherwise a default directory is used.

        If 'text' is specified and true, the file is opened in text
        mode.  Else (the default) the file is opened in binary mode.

        If any of 'suffix', 'prefix' and 'dir' are not None, they must be the
        same type.  If they are bytes, the returned name will be bytes; str
        otherwise.

        The file is readable and writable only by the creating user ID.
        If the operating system uses permission bits to indicate whether a
        file is executable, the file is executable by no one. The file
        descriptor is not inherited by children of this process.

        Caller is responsible for deleting the file when done with it.

### mktemp
        User-callable function to return a unique temporary file name.  The
        file is not created.

        Arguments are similar to mkstemp, except that the 'text' argument is
        not accepted, and suffix=None, prefix=None and bytes file names are not
        supported.

        THIS FUNCTION IS UNSAFE AND SHOULD NOT BE USED.  The file name may
        refer to a file that did not exist at some point, but by the time
        you get around to creating it, someone else may have beaten you to
        the punch.

## DATA
    TMP_MAX = 238328
    __all__ = ['NamedTemporaryFile', 'TemporaryFile', 'SpooledTemporaryFil...
    tempdir = None

## FILE
    /usr/lib/python3.10/tempfile.py


