Skip to content
Snippets Groups Projects
Commit 7a2dc23f authored by Paul McCarthy's avatar Paul McCarthy :mountain_bicyclist:
Browse files

New module tempdir, which contains tempdir function

parent d74b3d88
No related branches found
No related tags found
No related merge requests found
#!/usr/bin/env python
#
# tempdir.py - Utilities for working with temporary directories.
#
# Author: Paul McCarthy <pauldmccarthy@gmail.com>
#
"""This module contains utilities for working with temporary files and
directories. It currently only contains one function:
.. autosummary::
:nosignature:
tempdir
"""
import os
import shutil
import tempfile
import contextlib
@contextlib.contextmanager
def tempdir():
"""Returns a context manager which creates and returns a temporary
directory, and then deletes it on exit.
"""
testdir = tempfile.mkdtemp()
prevdir = os.getcwd()
try:
os.chdir(testdir)
yield testdir
finally:
os.chdir(prevdir)
shutil.rmtree(testdir)
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment