Python Forum

Full Version: How to combine file names into a list from multiple directories?
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
My files in folders are structures as follows:
Folder1
Filea
Fileb
Filec

Folder2
Filea
Fileb
Filec

Folder3
Filea
Fileb
Filec


I am trying to loop through each folder and build a list of the files with the path directory so that I can pass this list to another function that will process each list of files with the same name as the idea is I need to in the end combine the data from files with the same name.

file_list = []

for filename in os.listdir(path):
    print(filename)
    for root, dirs,files in os.walk(path1):
        for f in files:
            print(f)
            if f == filename:
                fullpath = os.path.join(root,f)
                filename = os.path.basename(fullpath)
                filelist = file_list.append(fullpath)
                #print(filelist)
I am struggling to get the logic right because I am not sure how am I going to pass the list of [path1_filea, path2_filea,path3_filea] and the pass the next list of files [path1_fileb, path2_fileb, path3_file] so that the next function will process each list one by one.
I hope this will help:
#!/usr/bin/python3
import os, pprint

path = "test/"
file_list = {}

for dirname in os.listdir(path):
    print("dir:", dirname)
    for root,dirs,files in os.walk(path + dirname):
        for filename in files:
            print("file:", filename)
            if filename not in file_list:
                file_list[filename]  = []
            file_list[filename].append(os.path.join(root, filename))

print()
pprint.pprint(file_list)
Output:
dir: folder2 file: filea file: fileb file: filec dir: folder3 file: filea file: fileb file: filec dir: folder1 file: filea file: fileb file: filec {'filea': ['test/folder2/filea', 'test/folder3/filea', 'test/folder1/filea'], 'fileb': ['test/folder2/fileb', 'test/folder3/fileb', 'test/folder1/fileb'], 'filec': ['test/folder2/filec', 'test/folder3/filec', 'test/folder1/filec']}
using pathlib, to get list of files in a directory

from pathlib import Path

def get_filelist(basepath):
    return [fillename for filename in basepath.iterdir() if filename.is_file()]
(Jun-25-2019, 05:33 PM)heiner55 Wrote: [ -> ]I hope this will help:
#!/usr/bin/python3
import os, pprint

path = "test/"
file_list = {}

for dirname in os.listdir(path):
    print("dir:", dirname)
    for root,dirs,files in os.walk(path + dirname):
        for filename in files:
            print("file:", filename)
            if filename not in file_list:
                file_list[filename]  = []
            file_list[filename].append(os.path.join(root, filename))

print()
pprint.pprint(file_list)
Output:
dir: folder2 file: filea file: fileb file: filec dir: folder3 file: filea file: fileb file: filec dir: folder1 file: filea file: fileb file: filec {'filea': ['test/folder2/filea', 'test/folder3/filea', 'test/folder1/filea'], 'fileb': ['test/folder2/fileb', 'test/folder3/fileb', 'test/folder1/fileb'], 'filec': ['test/folder2/filec', 'test/folder3/filec', 'test/folder1/filec']}


Thank you this worked.