Reputation: 1511
Using os.walk()
, I managed to return every sub directories at the maximum depth:
>>> import os
>>> sites = [x[0] for x in os.walk('./')]
>>> print(sites)
['./', './Saudi_arabia', './Saudi_arabia/Periodic_barchans', './Saudi_arabia/Finger_rocks', './Mars', './Niger', './Maroc', './Algeria', './China']
However, I'd like to filter the ones that have directories in themselves.
For example, ./Saudi_arabia/
should not appear because it contains ./Saudi_arabia/Periodic_barchans
and ./Saudi_arabia/Finger_rocks
.
How would you do that ?
Upvotes: 1
Views: 28
Reputation: 106455
You can filter out entries with non-empty lists of sub-directories, which are stored as the second items of the tuples generated by os.walk
:
sites = [root for root, dirs, _ in os.walk('.') if not dirs]
Upvotes: 1