Honci Tuljic
Honci Tuljic

Reputation: 23

Extract directories from a list of paths

I have a list of paths to files, like this:

paths = [
    'A/B/b.py',
    'A/B/C/c1.py',
    'A/B/C/c2.py',
    'M/N/O/o1.py',
    'M/N/O/o2.py',
    'M/N/P/p1.py',
    'M/N/P/p2.py',
    'M/N/P/R/r2.py'
]

I want to transform that to a directory listing like this:

only_dirs = [
    'A',
    'A/B',
    'A/B/C',
    'M',
    'M/N',
    'M/N/O',
    'M/N/P',
    'M/N/P/R',
]

Those paths don't exist on the disk, they're just strings collected from DB, and I want to get a listing of directories in order to filter files according to any dir in the paths. What is the cleanest way of doing this?

Upvotes: 2

Views: 96

Answers (2)

kaspermoerch
kaspermoerch

Reputation: 16570

EDITED ANSWER

As pointed out in a comment by thefourtheye, my first answer was incorrect.

Here is a new solution to the problem:

only_dirs = []

for path in paths:
    current = path[:path.rfind('/')]
    while len(current) > 0:
        if current not in only_dirs:
            only_dirs.append(current)
        current = current[:current.rfind('/')]

only_dirs.sort()
print only_dirs

Upvotes: 0

thefourtheye
thefourtheye

Reputation: 239443

result = set()
for items in paths:
    splitted = items.split("/")[:-1]
    for idx in xrange(1, len(splitted) + 1):
        result.add("/".join(splitted[:idx]))

only_dirs = sorted(result)
print only_dirs

Output

['A', 'A/B', 'A/B/C', 'M', 'M/N', 'M/N/O', 'M/N/P', 'M/N/P/R']

Upvotes: 4

Related Questions