from pathlib import Path def ensure_relative_path(file_path: str, relative_to: str | Path = None) -> Path: """ Convert the given path to a relative path. :param file_path: The path to check and possibly convert. :param relative_to: Optional path to which the given path should be made relative. If not provided, the current working directory is used. :return: The relative path. """ path = Path(file_path) if path.is_absolute(): base_path = Path(relative_to) if relative_to else Path.cwd() return path.relative_to(base_path) return path