Context-manager that temporarily changes to a new working directory.

def excursion(directory):
    """Context-manager that temporarily changes to a new working directory."""
    old_dir = os.getcwd()
    try:
        os.chdir(directory)
        yield
    finally:
        os.chdir(old_dir)