mirror of
https://github.com/Hestia-Homes/Model.git
synced 2026-06-08 11:17:27 +00:00
17 lines
616 B
Python
17 lines
616 B
Python
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
|