Module tempfile
[hide private]
[frames] | no frames]

Module tempfile

Temporary files.

This module provides generic, low- and high-level interfaces for
creating temporary files and directories.  The interfaces listed
as "safe" just below can be used without fear of race conditions.
Those listed as "unsafe" cannot, and are provided for backward
compatibility only.

This module also provides some data items to the user:

  TMP_MAX  - maximum number of names that will be tried before
             giving up.
  template - the default prefix for all temporary names.
             You may change this to control the default prefix.
  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 [hide private]
_RandomNameSequence
An instance of _RandomNameSequence generates an endless sequence of unpredictable strings which can safely be incorporated into file names.
_TemporaryFileWrapper
Temporary file wrapper
Functions [hide private]
 
_set_cloexec(fd)
 
_exists(fn)
 
_candidate_tempdir_list()
Generate a list of candidate temporary directories which _get_default_tempdir will try.
 
_get_default_tempdir()
Calculate the default directory to use for temporary files.
 
_get_candidate_names()
Common setup sequence for all user-callable interfaces.
 
_mkstemp_inner(dir, pre, suf, flags)
Code common to mkstemp, TemporaryFile, and NamedTemporaryFile.
 
gettempprefix()
Accessor for tempdir.template.
 
gettempdir()
Accessor for tempdir.tempdir.
 
mkstemp(suffix='', prefix='tmp', dir=None, text=False)
mkstemp([suffix, [prefix, [dir, [text]]]]) User-callable function to create and return a unique temporary file.
 
mkdtemp(suffix='', prefix='tmp', dir=None)
mkdtemp([suffix, [prefix, [dir]]]) User-callable function to create and return a unique temporary directory.
 
mktemp(suffix='', prefix='tmp', dir=None)
mktemp([suffix, [prefix, [dir]]]) User-callable function to return a unique temporary file name.
 
NamedTemporaryFile(mode='w+b', bufsize=-1, suffix='', prefix='tmp', dir=None)
Create and return a temporary file.
 
TemporaryFile(mode='w+b', bufsize=-1, suffix='', prefix='tmp', dir=None)
Create and return a temporary file.
Variables [hide private]
  _text_openflags = 131266
  _bin_openflags = 131266
  TMP_MAX = 238328
  template = 'tmp'
  _once_lock = _allocate_lock()
  _name_sequence = None
  tempdir = None

Imports: _os, _errno, _Random, _Folder, _Folders, _fcntl, _thread, _allocate_lock, _stat


Function Details [hide private]

_get_default_tempdir()

 

Calculate the default directory to use for temporary files. This routine should be called exactly once.

We determine whether or not a candidate temp dir is usable by trying to create and write to a file in that directory. If this is successful, the test file is deleted. To prevent denial of service, the name of the test file must be randomized.

mkstemp(suffix='', prefix='tmp', dir=None, text=False)

 

mkstemp([suffix, [prefix, [dir, [text]]]]) 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 specified, the file name will end with that suffix, otherwise there will be no suffix.

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

If 'dir' is specified, 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. On some operating systems, this makes no difference.

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.

mkdtemp(suffix='', prefix='tmp', dir=None)

 

mkdtemp([suffix, [prefix, [dir]]]) 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.

mktemp(suffix='', prefix='tmp', dir=None)

 

mktemp([suffix, [prefix, [dir]]]) User-callable function to return a unique temporary file name. The file is not created.

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

This function is unsafe and should not be used. The file name refers 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.

NamedTemporaryFile(mode='w+b', bufsize=-1, suffix='', prefix='tmp', dir=None)

 

Create and return a temporary file. Arguments: 'prefix', 'suffix', 'dir' -- as for mkstemp. 'mode' -- the mode argument to os.fdopen (default "w+b"). 'bufsize' -- the buffer size argument to os.fdopen (default -1). 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 file.name. The file will be automatically deleted when it is closed.

TemporaryFile(mode='w+b', bufsize=-1, suffix='', prefix='tmp', dir=None)

 

Create and return a temporary file. Arguments: 'prefix', 'suffix', 'dir' -- as for mkstemp. 'mode' -- the mode argument to os.fdopen (default "w+b"). 'bufsize' -- the buffer size argument to os.fdopen (default -1). 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.