@contextlib.contextmanager
def cd(newpath):
 oldpath = os.getcwd()
 os.chdir(newpath)
 try:
  (yield)
 finally:
  try:
   os.chdir(oldpath)
  except OSError:
   pass

@ contextlib . contextmanager def cd ( newpath ) : oldpath = os . getcwd ( ) os . chdir ( newpath ) try : ( yield ) finally : try : os . chdir ( oldpath ) except OSError : pass
context manager that keeps directory state when calling remote operations .