Last active
January 1, 2016 12:18
-
-
Save fridgei/8143250 to your computer and use it in GitHub Desktop.
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
import os | |
class DirectoryScope(object): | |
def __init__(self, new_directory): | |
self.old_directory = os.path.abspath(os.getcwd()) | |
self.new_directory = os.path.abspath(new_directory) | |
def __enter__(self): | |
return os.chdir(self.new_directory) | |
def __exit__(self, exc_type, exc_val, exc_tb ): | |
os.chdir(self.old_directory) | |
return exc_type is None | |
os.chdir('/var/') | |
print os.getcwd() | |
with DirectoryScope('/etc/'): | |
print os.getcwd() | |
print os.getcwd() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment