Skip to content

Instantly share code, notes, and snippets.

@ralphbean
Created March 11, 2013 17:23
Show Gist options
  • Save ralphbean/5135935 to your computer and use it in GitHub Desktop.
Save ralphbean/5135935 to your computer and use it in GitHub Desktop.
class directory(object):
""" pushd/popd context manager.
Use like:
>>> with directory("/tmp/"):
... pass # do some stuff
"""
def __init__(self, newPath):
self.newPath = newPath
def __enter__(self, *args, **kw):
self.savedPath = os.getcwd()
os.chdir(self.newPath)
def __exit__(self, *args, **kw):
os.chdir(self.savedPath)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment