Skip to content

Instantly share code, notes, and snippets.

@theY4Kman
Created September 1, 2012 19:01
Show Gist options
  • Save theY4Kman/3583442 to your computer and use it in GitHub Desktop.
Save theY4Kman/3583442 to your computer and use it in GitHub Desktop.
pushd context manager
@contextmanager
def pushd(new_dir):
"""
A context manager that implements the `pushd` command, letting you run a
block of commands while in a different directory
"""
old_dir = os.getcwd()
os.chdir(new_dir)
try:
yield old_dir
finally:
os.chdir(old_dir)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment