@contextlib.contextmanager<EOL>def temporary_chdir(new_dir):
old_dir = os.getcwd()<EOL>os.chdir(new_dir)<EOL>try:<EOL><INDENT>yield<EOL><DEDENT>finally:<EOL><INDENT>os.chdir(old_dir)<EOL><DEDENT>
Like os.chdir(), but always restores the old working directory

For example, code like this...

    old_curdir = os.getcwd()
    os.chdir('stuff')
    do_some_stuff()
    os.chdir(old_curdir)

...leaves the current working directory unchanged if do_some_stuff()
raises an error, so it should be rewritten like this:

    old_curdir = os.getcwd()
    os.chdir('stuff')
    try:
        do_some_stuff()
    finally:
        os.chdir(old_curdir)

Or equivalently, like this:

    with utils.temporary_chdir('stuff'):
        do_some_stuff()