def ensure_directory_exists(directory):
  '''Ensures `directory` exists. May make `directory` and intermediate dirs.
  Raises RuntimeError if `directory` is a file.
  '''
  if not os.path.exists(directory):
    os.makedirs(directory)
  elif os.path.isfile(directory):
    raise RuntimeError('Path %s is a file, not a directory.' % directory)
Ensures `directory` exists. May make `directory` and intermediate dirs.
  Raises RuntimeError if `directory` is a file.