code: def set_cwd(new_path):
      
      try:
          curdir = os.getcwd()
      except OSError:
          curdir = new_path
      try:
          os.chdir(new_path)
          yield
      finally:
          os.chdir(curdir)
docs: Usage:
  
          with set_cwd('/some/dir'):
              walk_around_the_filesystem()