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:

class testpath.tempdir.NamedFileInTemporaryDirectory(filename, mode='w+b', bufsize=-1, **kwds)

Open a file named filename in a temporary directory.

This context manager is preferred over tempfile.NamedTemporaryFile when 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.

Usage example:

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
class testpath.tempdir.TemporaryWorkingDirectory(suffix=None, prefix=None, dir=None)

Creates a temporary directory and sets the cwd to that directory. Automatically reverts to previous cwd upon cleanup.

Usage example:

with TemporaryWorkingDirectory() as tmpdir:
    ...