Created
June 22, 2015 17:35
-
-
Save vsudilov/6f2b1a6d2086b14ad941 to your computer and use it in GitHub Desktop.
change directory context manager
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
class ChangeDir: | |
"""Context manager for changing the current working directory""" | |
def __init__(self, newPath): | |
self.newPath = os.path.expanduser(newPath) | |
def __enter__(self): | |
self.savedPath = os.getcwd() | |
os.chdir(self.newPath) | |
def __exit__(self, etype, value, traceback): | |
os.chdir(self.savedPath) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment