Skip to content

Instantly share code, notes, and snippets.

@vsudilov
Created June 22, 2015 17:35
Show Gist options
  • Save vsudilov/6f2b1a6d2086b14ad941 to your computer and use it in GitHub Desktop.
Save vsudilov/6f2b1a6d2086b14ad941 to your computer and use it in GitHub Desktop.
change directory context manager
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