old_contents: import os
  import shutil
  
  
  class Workspace(object):
      def __init__(self, workspace, overwrite=False):
          self.workspace = workspace
          self.overwrite = overwrite
          self.prev_dir = None
  
      def __enter__(self):
          if self.overwrite and os.path.isdir(self.workspace):
              shutil.rmtree(self.workspace)
          os.makedirs(self.workspace)
          self.prev_dir = os.getcwd()
          os.chdir(self.workspace)
  
      def __exit__(self, *exc):
          os.chdir(self.prev_dir)
  
new_contents: import os
  import shutil
  
  
  class Workspace(object):
      def __init__(self, workspace, overwrite=False):
          self.workspace = workspace
          self.overwrite = overwrite
          self.prev_dir = os.getcwd()
  
      def __enter__(self):
          if os.path.relpath(self.workspace) != os.curdir:
              workspace_exists = os.path.isdir(self.workspace)
              if self.overwrite and workspace_exists:
                  shutil.rmtree(self.workspace)
              if self.overwrite or not workspace_exists:
                  os.makedirs(self.workspace)
              os.chdir(self.workspace)
  
      def __exit__(self, *exc):
          os.chdir(self.prev_dir)
  
subject: Handle workspace dir of '.'