Python 3.4+pathlibterm
pathlib.PurePath.parts
path components
A tuple of path components: ('/', 'usr', 'bin', 'python3') for '/usr/bin/python3'.
Syntax
PurePath(...).parts
The root (on Windows, the drive plus root, 'C:\\') comes as a separate first item. The reverse is passing the parts to the constructor: PurePath(*parts). parts is handy for checking whether a path lies inside a given directory or for finding a particular component.
Examples
from pathlib import PurePosixPath as P, PureWindowsPath as W
print(P('/usr/bin/python3').parts) # → ('/', 'usr', 'bin', 'python3')
print(W('C:/Users/anna').parts) # → ('C:\\', 'Users', 'anna')
print('bin' in P('/usr/bin/python3').parts) # → True
See also
Open in the full glossarydocs.python.orgReport an inaccuracy