Skip to content

Instantly share code, notes, and snippets.

@nitely
Created August 7, 2015 12:49
Show Gist options
  • Save nitely/19184ffabb7cdfafde20 to your computer and use it in GitHub Desktop.
Save nitely/19184ffabb7cdfafde20 to your computer and use it in GitHub Desktop.
python pushd
from contextlib import contextmanager
import os
@contextmanager
def pushd(new_dir):
prev_dir = os.getcwd()
os.chdir(new_dir)
yield
os.chdir(prev_dir)
if __name__ == "__main__":
with pushd('./my_dir'):
print(os.getcwd()) # ./my_dir
with pushd('./my_dir/my_other_dir'):
print(os.getcwd()) # ./my_dir/my_other_dir
print(os.getcwd()) # ./my_dir
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment