Open this example in the interactive library
Source code
from pathlib import PurePosixPath
print([str(p) for p in PurePosixPath('/a/b/c.txt').parents])Sample output
['/a/b', '/a', '/']

Walk path parents. A self-contained example using the Python standard library.
Open this example in the interactive library
from pathlib import PurePosixPath
print([str(p) for p in PurePosixPath('/a/b/c.txt').parents])['/a/b', '/a', '/']