I am making a dataset, which is in a way such that for one thing like "apple" there is a folder named "apples" in the root folder(contains multiple folders) that contains only images of apples and so on.
I want to make a csv file in which it has all the filenames as one column and other as the folder name.
I tried this but it is entering data row-wise
from PIL import Image
import csv
import os
subdirs = [x[0] for x in os.walk('Training images')]
print(subdirs)
data=[]
with open('images.csv', 'w', newline='') as writeFile:
writer = csv.writer(writeFile)
for i in range(len(subdirs)):
for filename in os.listdir(subdirs[i]):
data.append(filename)
writer.writerow(data)
data=[]
writeFile.close()
As written here,
writerow()
function can be used with lists. In your example,data=[]
is the list and it is putted intowriterow()
.You append only one item:
data.append(filename)
. Just append another:data.append(dirname)
.Or without temp variable
data
at all (recommended, less code = simpler to understand):