Reputation: 433
Say i have a path like this
Folder1/Folder2/Folder3/Folder4/
Doesn't necessarily have to have the final slash. How do i extract just "Folder2".
Upvotes: 1
Views: 470
Reputation: 2473
Using the 'pathlib' module of the standard library:
>>> import pathlib
>>> pathlib.PurePath('Folder1/Folder2/Folder3/Folder4/').parts[1]
you get:
'Folder2'
Or for subdirs paths:
>>> pathlib.PurePath('Folder1/Folder2/Folder3/Folder4/').parents[1]
You get:
PurePosixPath('Folder1/Folder2')
Upvotes: 1