code: def _open_for_write(filename):
      
      dirname = os.path.dirname(filename)
      pathlib.Path(dirname).mkdir(parents=True, exist_ok=True)
  
      return io.open(filename, 'w')
docs: Opens filename for writing, creating the directories if needed.