Skip to content

Instantly share code, notes, and snippets.

@T1T4N
Last active February 26, 2018 08:59
Show Gist options
  • Save T1T4N/26732d878baa2704112b948924450c59 to your computer and use it in GitHub Desktop.
Save T1T4N/26732d878baa2704112b948924450c59 to your computer and use it in GitHub Desktop.
Python pushd implementation using context manager
import os
from contextlib import contextmanager
@contextmanager
def pushd(newdir):
old_path = os.getcwd()
os.chdir(newdir)
try:
yield
finally:
os.chdir(old_path)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment