def tmp_chdir(new_path):
    """Change directory temporarily and return when done."""
    prev_cwd = os.getcwd()
    os.chdir(new_path)
    try:
        yield
    finally:
        os.chdir(prev_cwd)
Change directory temporarily and return when done.