Python 3’s pathlib module makes this straightforward with its Path.parents attribute. For example:
from pathlib import Path
root = Path('/path/to/root')
child = root / 'some"https://stackoverflow.com/"child"https://stackoverflow.com/"dir'
other = Path('/some/other/path')
Then:
>>> root in child.parents
True
>>> other in child.parents
False