def safe_open(path, mode = 'r'):
  """Open the path safely. If in write model, make intermediate directories.

  Args:
    path: path to open
    mode: mode to use

  Returns:
    file handler
  """
  if isinstance(path, pathlib.PurePath):
    path = str(path)

  if 'w' in mode:
    directory = os.path.dirname(path)
    tf.io.gfile.makedirs(directory)
    return tf.io.gfile.GFile(path, mode)
  else:
    return tf.io.gfile.GFile(path, mode)
Open the path safely. If in write model, make intermediate directories.

  Args:
    path: path to open
    mode: mode to use

  Returns:
    file handler
  
language/serene/util.py