Source code for py_research.files

"""Helper functions for handling files and directories."""

from pathlib import Path


[docs] def ensure_dir_exists(path: Path | str): """Make sure the given path exists and is a directory. Create dir if necessary. Then return the path unchanged. Args: path: Path to ensure exists. Returns: The given path. """ path = Path(path).absolute() p = Path(path.parts[0]) for d in path.parts[1:]: p = p / d if not p.is_dir(): if not p.exists(): p.mkdir() else: raise NotADirectoryError() return path