I'm trying to extract subpath from a list of paths using python:
path structure looks like this:
path= '/a/b/c/d/e/f/g/h/image.png'
This is what I'm looking for:
'f/g/h/image.png'
The relative_to method on Path works like this. You can use str to turn the result into a string, if you prefer.
>>> from pathlib import Path
>>> path = Path('/a/b/c/d/e/f/g/h/image.png')
>>> path.relative_to('/a/b/c/d/e')
PosixPath('f/g/h/image.png')
>>> str(_)
'f/g/h/image.png'
Documentation: https://docs.python.org/3/library/pathlib.html#pathlib.PurePath.relative_to