Let's say I have a Path object like so:
from pathlib import Path
path = Path('/Users/Me/Desktop/Media/Pictures')
On posix systems, this returns a PosixPath
object, and on Windows, a WindowsPath
object.
Is there a nice, build-in Pythonic way to get this natively using pathlib
?
>>> segments
['Users', 'Me', 'Desktop', 'Media', 'Pictures']
(Or if not a list, a generator is fine, too).
I know I could do this, but I don't like that it relies on str
type conversion, and though I can't test this on Windows, I'm not confident it will handle backslash escaping well:
[x for x in str(path).split(pathlib.os.sep)]