TempfileContext
(class from pyomo.common.tempfiles)
- class pyomo.common.tempfiles.TempfileContext(manager)[source]
Bases:
objectA context for managing collections of temporary files
Instances of this class hold a “temporary file context”. That is, this records a collection of temporary file system objects that are all managed as a group. The most common use of the context is to ensure that all files are deleted when the context is released.
This class replicates a significant portion of the
tempfilemodule interface.Instances of this class may be used as context managers (with the temporary files / directories getting automatically deleted when the context manager exits).
Instances will also attempt to delete any temporary objects from the filesystem when the context falls out of scope (although this behavior is not guaranteed for instances existing when the interpreter is shutting down).
Methods
__init__(manager)add_tempfile(filename[, exists])Declare the specified file/directory to be temporary.
create_tempdir([suffix, prefix, dir])Create a unique temporary directory.
create_tempfile([suffix, prefix, text, dir])Create a unique temporary file.
Return the default name of the directory used for temporary files.
Same as
gettempdir(), but the return value isbytesReturn the filename prefix used to create temporary files.
Same as
gettempprefix(), but the return value isbytesmkdtemp([suffix, prefix, dir])Create a unique temporary directory using
tempfile.mkdtemp()mkstemp([suffix, prefix, dir, text])Create a unique temporary file using
tempfile.mkstemp()release([remove])Release this context
Member Documentation
- add_tempfile(filename, exists=True)[source]
Declare the specified file/directory to be temporary.
This adds the specified path as a “temporary” object to this context’s list of managed temporary paths (i.e., it will be potentially be deleted when the context is released (see
release()).
- create_tempdir(suffix=None, prefix=None, dir=None)[source]
Create a unique temporary directory.
The file name is generated as in
tempfile.mkdtemp().
- create_tempfile(suffix=None, prefix=None, text=False, dir=None)[source]
Create a unique temporary file.
The file name is generated as in
tempfile.mkstemp().Any file handles to the new file (e.g., from
mkstemp()) are closed.
- gettempdir()[source]
Return the default name of the directory used for temporary files.
This method returns the first non-null location returned from:
This context’s
tempdir(i.e.,self.tempdir)This context’s manager’s
tempdir(i.e.,self.manager().tempdir)
- Returns:
dir – The default directory to use for creating temporary objects
- Return type:
- gettempdirb()[source]
Same as
gettempdir(), but the return value isbytes
- gettempprefixb()[source]
Same as
gettempprefix(), but the return value isbytes
- mkdtemp(suffix=None, prefix=None, dir=None)[source]
Create a unique temporary directory using
tempfile.mkdtemp()Parameters are handled as in
tempfile.mkdtemp(), with the exception that the new file is created in the directory returned bygettempdir()
- mkstemp(suffix=None, prefix=None, dir=None, text=False)[source]
Create a unique temporary file using
tempfile.mkstemp()Parameters are handled as in
tempfile.mkstemp(), with the exception that the new file is created in the directory returned bygettempdir()- Returns:
fd (int) – the opened file descriptor
fname (str or bytes) – the absolute path to the new temporary file