code: def temporary_chdir(new_dir):
      
      old_dir = os.getcwd()
      os.chdir(new_dir)
      try:
          yield
      finally:
          os.chdir(old_dir)
docs: 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()