简体   繁体   中英

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!

Here is a possible solution in Python 3.6+ (3.4+ because of pathlib , and 3.6+ because of f-strings ):

from pathlib import Path
import csv


folder = Path('/path/to/main/Folder'):
csvdir = Path('/path/to/store/csv/files')
participants = [1, 2, 3, 4]
headers = ['participant', 'imagefile']

for participant in participants:
    participant_folder = folder / f'Folder{participant}'

    # Do we have a Folder for participant number? 
    if participant_folder.is_dir():
        # create a csv file in csvdir for participant
        with Path(csvdir / f'imagefiles{participant}.csv').open('w') as f:
            csvwriter = csv.writer(f, quote=csv.QUOTEALL)
            
            cswriter.writerow(headers)

            for image in participant_folder.glob('*.jpg'):
                csvwriter.writerow([participant, image])

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))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM