I have two folders cats and dogs with 5 jpeg images in each cat1.jpeg cat2.jpeg..... dog1.jpeg dog2.jpeg etc...
I want to write a CSV file that looks like this
1,./images/trainnew/dogs/dogs2.jpg,dogs 2,./images/trainnew/dogs/dogs1.jpg,dogs 3,./images/trainnew/cats/cats2.jpg,cats 4,./images/trainnew/cats/cats1.jpg,cats
Where entry dogs and cats at then end of each line refers to the name of the folders dogs and cats within the folder trainnew/
using the code below
from glob import glob
import os
count =1
f = open('test7.csv', 'w')
for filename in glob('./images/trainnew/**/*.jpg', recursive=True):
f.write(str(count) + ',' + filename + ',\n')
print(filename)
count +=1
===================
My test7.csv looks like this
1,./images/trainnew/dogs/dogs2.jpg, 2,./images/trainnew/dogs/dogs1.jpg, 3,./images/trainnew/cats/cats2.jpg, 4,./images/trainnew/cats/cats1.jpg,
How do i get the respective directory names and print at the end of each of these lines respectively in the csv file.
The
os.path.dirname(filename)line retrieves the directory of the current file (like './images/trainnew/dogs' or './images/trainnew/cats'), andos.path.basename(dirname)give u the base name of that path (like 'dogs' or 'cats').