Context manager returns a path created by mkdtemp and cleans it up afterwards.

def temp_directory(*args, **kwargs):
    """
    Context manager returns a path created by mkdtemp and cleans it up afterwards.
    """

    path = tempfile.mkdtemp(*args, **kwargs)
    try:
        yield path
    finally:
        shutil.rmtree(path)