Skip to content

Instantly share code, notes, and snippets.

@brickZA
Created September 6, 2011 12:37
Show Gist options
  • Save brickZA/1197432 to your computer and use it in GitHub Desktop.
Save brickZA/1197432 to your computer and use it in GitHub Desktop.
A TempDir object that is compatible with the 'with' statment use, similar to tempfile.NamedTemporaryFile
import tempfile
import shutil
class TempDir(object):
def __init__(self, delete=True, **kwargs):
self.tempdir = tempfile.mkdtemp(**kwargs)
self.delete = delete
def __enter__(self):
return self.tempdir
def __exit__(self, type, value, traceback):
if self.delete:
shutil.rmtree(self.tempdir)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment