'Loop through sub-folder and save to .csv in Python

I have a folder called Folder structured like this:

Folder/
├── Folder1
│   ├── image1.jpg
│   ├── image2.jpg
│   ├── image3.jpg
│   ├── image4.jpg
│   └── image5.jpg
├── Folder2
│   ├── image1.jpg
│   ├── image2.jpg
│   ├── image3.jpg
│   ├── image4.jpg
│   └── image5.jpg
├── Folder3
│   ├── image1.jpg
│   ├── image2.jpg
│   ├── image3.jpg
│   ├── image4.jpg
│   └── image5.jpg
└── Folder4
    ├── image1.jpg
    ├── image2.jpg
    ├── image3.jpg
    ├── image4.jpg
    └── image5.jpg

I have a code that prints the Folders along with the images in each folder.

rootDir = '.'
for dirName, subdirList, fileList in os.walk(rootDir):
    print('Found directory: %s' % dirName)
    for fname in fileList:
        print('\t%s' % fname)"   

However, I want to write a code to loop through the sub-folders and save each of the 5 images to a .csv file. For example, if I have participant 1 I want a .csv file containing the images from Folder1 if I have participant 2 I want a .csv file containing the images from Folder2, and so on.

I think I might need to create an empty list, and then save to a .csv file like below:

lst = []

cols = ['participant', 'imagefile']

pd.DataFrame(lst,columns=cols).to_csv('imagefiles.csv', index=False)

Any help would be really appreciated!



Solution 1:[1]

Use the below code to get every jpg file names in subfolders to a csv file.

import csv
import os
import glob

root_folder = '/path/to/root_folder'
out_csv_name = '/path/to/out.csv'
dir_list = os.listdir(root_folder)

filename_list = []

for each_dir in dir_list:

    with open(out_csv_name, mode='a') as crop_file_name:

        csvwriter = csv.writer(crop_file_name, delimiter=',', quotechar='"', quoting=csv.QUOTE_MINIMAL)

        for image in glob.glob(os.path.join(root_folder,each_dir+"/*.jpg")):

            csvwriter.writerow([os.path.basename(image)])
            filename_list.append(os.path.basename(image))

print(len(filename_list))

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 akash