Skip to content

Commit

Permalink
Add chdir() context manager function
Browse files Browse the repository at this point in the history
  • Loading branch information
taldcroft committed Sep 13, 2009
1 parent d613511 commit 4d593d3
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 1 deletion.
21 changes: 21 additions & 0 deletions Ska/File.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,27 @@
import re
import subprocess
import glob
import contextlib

@contextlib.contextmanager
def chdir(dirname=None):
"""
Context manager to run block within `dirname` directory. The current
directory is restored even if the block raises an exception.
>>> with chdir(dirname):
>>> print "Directory within chdir() context:", os.getcwd()
>>> print "Directory after chdir() context:", os.getcwd()
:param dirname: Directory name
"""
curdir = os.getcwd()
try:
if dirname is not None:
os.chdir(dirname)
yield
finally:
os.chdir(curdir)

class TempDir(object):
"""Create a temporary directory that gets automatically removed. Any
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
description='Various file utilities',
author_email = '[email protected]',
py_modules = ['Ska.File'],
version='0.02',
version='0.03',
zip_safe=False,
namespace_packages=['Ska'],
packages=['Ska'],
Expand Down

0 comments on commit 4d593d3

Please sign in to comment.