How can I extend file paths for a dictionary

Viewed 68
import plazy


txt_filter = lambda x : True if x.endswith('') else False
file_paths: list = plazy.list_files(root='/data/', filter_func=txt_filter, is_include_root=True)

print(file_paths)

output:

 ["/data/subdir1/subdir1_1/file1.txt","/data/subdir2/subdir2_1/file2.txt", "/data/subdir2/subdir2_1/file1.txt", "/data/subdir3/subdir3_1/subdir3_2/file1.txt"]

How can I extend these paths for a specific dictionary. I want it to look like this

{ 
"data": 
    "subdir1" : { "subdir1_1": ["file1.txt"]},
    "subdir2" : { "subdir2_1": ["file1.txt", "file2.txt"]},
    "subdir3" : { "subdir3_1":
                     { "subdir3_2": ["file1.txt"]}
                 }
}
2 Answers

I think one way to address this is by using plazy.list_files in a limited depth first (to get top level dirs) and recurse manually, rather than letting it get the whole tree.

Some pseudo code to illustrate...

topdirs = getdirs(/root)

foreach dir
  children = getdirs(/dir)
  leaves = gettxtfiles(/dir/)

As your program recurses into the structure it builds it map the way you want it.

I took care of it without using Plazy.

path = '/data/'

import os
import pprint

def f(path):
    if os.path.isdir(path):
        d ,l = {}, []
        for name in os.listdir(path):
            if os.path.isdir(os.path.join(path, name)):
                d[name] = f(os.path.join(path, name))
            else:
                l.append(name)
                d = l
    return d

pprint.pprint(f(path))

Output

{ 
"data": 
    "subdir1" : { "subdir1_1": ["file1.txt"]},
    "subdir2" : { "subdir2_1": ["file1.txt", "file2.txt"]},
    "subdir3" : { "subdir3_1":
                     { "subdir3_2": ["file1.txt"]}
                 }
}
Related