Utilities for temporary directories¶
This module exposes
tempfile.TemporaryDirectory(), with a backported copy
so that it can be used on Python 2. In addition, it contains:
NamedFileInTemporaryDirectory(filename, mode='w+b', bufsize=-1, **kwds)¶
Open a file named filename in a temporary directory.
This context manager is preferred over
tempfile.NamedTemporaryFilewhen one needs to reopen the file, because on Windows only one handle on a file can be open at a time. You can close the returned handle explicitly inside the context without deleting the file, and the context manager will delete the whole directory when it exits.
Arguments mode and bufsize are passed to open. Rest of the arguments are passed to TemporaryDirectory.
with NamedFileInTemporaryDirectory('myfile', 'wb') as f: f.write('stuff') f.close() # You can now pass f.name to things that will re-open the file
TemporaryWorkingDirectory(suffix='', prefix='tmp', dir=None)¶
Creates a temporary directory and sets the cwd to that directory. Automatically reverts to previous cwd upon cleanup.
with TemporaryWorkingDirectory() as tmpdir: ...