libb.make_tmpdir

make_tmpdir(prefix=None)[source]

Context manager to wrap a temporary directory with auto-cleanup.

Parameters:

prefix (str) – Optional prefix directory for the temp dir.

Yields:

Path to the temporary directory.

Return type:

Path

Example:

>>> import os.path
>>> fpath = ""
>>> with make_tmpdir() as basedir:
...     fpath = os.path.join(basedir, 'temp.txt')
...     with open(fpath, "w") as file:
...         file.write("We expect the file to be deleted when context closes")
52
>>> try:
...     file = open(fpath, "w")
... except IOError as io:
...     raise Exception('File does not exist')
Traceback (most recent call last):
...
Exception: File does not exist