TempfileContext
(class from pyomo.common.tempfiles
)
-
class pyomo.common.tempfiles.TempfileContext(manager)[source]
Bases: object
A 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 tempfile
module 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).
-
__init__(manager)[source]
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. |
gettempdir ()
|
Return the default name of the directory used for temporary files. |
gettempdirb ()
|
Same as gettempdir() , but the return value is bytes |
gettempprefix ()
|
Return the filename prefix used to create temporary files. |
gettempprefixb ()
|
Same as gettempprefix() , but the return value is bytes |
mkdtemp ([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()
).
- Parameters:
filename (str) – the file / directory name to be treated as temporary
exists (bool) – if True
, the file / directory must already exist.
-
create_tempdir(suffix=None, prefix=None, dir=None)[source]
Create a unique temporary directory.
The file name is generated as in tempfile.mkdtemp()
.
- Returns:
dname – The absolute path of the new directory.
- Return type:
str or bytes
-
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.
- Returns:
fname – The absolute path of the new file.
- Return type:
str or bytes
-
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
)
tempfile.gettempdir()
- Returns:
dir – The default directory to use for creating temporary objects
- Return type:
str
-
gettempdirb()[source]
Same as gettempdir()
, but the return value is bytes
-
gettempprefix()[source]
Return the filename prefix used to create temporary files.
See tempfile.gettempprefix()
-
gettempprefixb()[source]
Same as gettempprefix()
, but the return value is bytes
-
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 by gettempdir()
- Returns:
dname – the absolute path to the new temporary directory
- Return type:
str or bytes
-
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 by gettempdir()
- Returns:
-
-
release(remove=True)[source]
Release this context
This releases the current context, potentially deleting all
managed temporary objects (files and directories), and resetting
the context to generate unique names.
- Parameters:
remove (bool) – If True
, delete all managed files / directories