Created
August 7, 2015 12:49
-
-
Save nitely/19184ffabb7cdfafde20 to your computer and use it in GitHub Desktop.
python pushd
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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