@contextlib.contextmanager<EOL>def chdir(new_dir):
cur_dir = os.getcwd()<EOL>_mkdir(new_dir)<EOL>os.chdir(new_dir)<EOL>try:<EOL><INDENT>yield<EOL><DEDENT>finally:<EOL><INDENT>os.chdir(cur_dir)<EOL><DEDENT>
stolen from bcbio.
Context manager to temporarily change to a new directory.

http://lucentbeing.com/blog/context-managers-and-the-with-statement-in-python/