Skip to content

Instantly share code, notes, and snippets.

@fridgei
Last active January 1, 2016 12:18
Show Gist options
  • Save fridgei/8143250 to your computer and use it in GitHub Desktop.
Save fridgei/8143250 to your computer and use it in GitHub Desktop.
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