Created
September 6, 2011 12:37
-
-
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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