Dev HelpersOpen library
← Back

Check if a path is lexically under a directory

Check if a path is lexically under a directory in Python.

Open this example in the interactive library

Source code

from pathlib import PurePosixPath
path = PurePosixPath('/project/reports/summary.json')
print(path.is_relative_to('/project'), path.is_relative_to('/archive'))

Sample output

True False