Add function to get the path segments of a path relative to another

This commit is contained in:
2026-04-19 19:38:30 +02:00
parent 3fd51d126f
commit f581774649
3 changed files with 100 additions and 2 deletions
+22 -1
View File
@@ -151,4 +151,25 @@ def non_empty_str(value: Union[str, None]) -> Union[str, None]:
if value.strip() == '':
return None
return value
return value
def get_relative_path_segments(path: pathlib.Path, root: pathlib.Path) -> list[str]:
'''
Returns the path segments relative to ``root``.
:param path: The path.
:type path: pathlib.Path
:param root: The root path.
:type root: pathlib.Path
:return: The path segments relative to ``root``.
:rtype: list[str]
'''
return list(
path.resolve()
.relative_to(
root.resolve()
)
.parts
)