python - How can I convert parent - child relationship into a dictionary? -
the code far is:
for root, dirs, files in os.walk(path): directory in dirs: if directory.endswith("gui"): # folders end gui print "parent is: ", (os.path.join(root, directory)) os.chdir(os.path.join(root, directory)) file in glob.glob("*.b"): # files end b print "child is: ", (file) dictionaryparentchild[directory] = file return dictionaryparentchild
current: code return 1 parent: 1 child desired: code should return 1 parent: many children
def dir_files_map(start_dir): import os dd = {} # create dictionary key folder root path # , values files in folder # filter files based on endswiths(string) clause root, subfolders, filenames in os.walk(start_dir): f in filenames: if f.endswith('.b'): dd.setdefault(root,[]).append(f) return dd
Comments
Post a Comment