Python Forum
How to combine file names into a list from multiple directories?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to combine file names into a list from multiple directories?
#1
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.
Reply
#2
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']}
Reply
#3
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()]
Reply
#4
(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.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Organization of project directories wotoko 3 430 Mar-02-2024, 03:34 PM
Last Post: Larz60+
  Navigating file directories and paths inside Jupyter Notebook Mark17 5 711 Oct-29-2023, 12:40 PM
Last Post: Mark17
  How can i combine these two functions so i only open the file once? cubangt 4 867 Aug-14-2023, 05:04 PM
Last Post: snippsat
  Listing directories (as a text file) kiwi99 1 840 Feb-17-2023, 12:58 PM
Last Post: Larz60+
  Find duplicate files in multiple directories Pavel_47 9 3,114 Dec-27-2022, 04:47 PM
Last Post: deanhystad
  rename same file names in different directories elnk 0 712 Nov-04-2022, 05:23 PM
Last Post: elnk
  How to combine multiple column values into 1? cubangt 15 2,840 Aug-11-2022, 08:25 PM
Last Post: cubangt
  I need to copy all the directories that do not match the pattern tester_V 7 2,442 Feb-04-2022, 06:26 PM
Last Post: tester_V
  Functions to consider for file renaming and moving around directories cubangt 2 1,758 Jan-07-2022, 02:16 PM
Last Post: cubangt
  How to combine multiple rows of strings into one using pandas? shantanu97 1 3,154 Aug-22-2021, 05:26 AM
Last Post: klllmmm

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020