pythonglobdirname

How to get extract directory name from the full filepath name of a file using glob recursive


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.


Solution

  • from glob import glob
    import os
    
    count = 1
    with open('test7.csv', 'w') as f:
        for filename in glob('./images/trainnew/**/*.jpg', recursive=True):  
            dirname = os.path.dirname(filename)  # Get the directory name of the file
            foldername = os.path.basename(dirname)  # Get the base name from that directory path
            f.write(str(count) + ',' + filename + ',' + foldername + '\n')
            print(filename)
            count +=1
    

    The os.path.dirname(filename) line retrieves the directory of the current file (like './images/trainnew/dogs' or './images/trainnew/cats'), and os.path.basename(dirname) give u the base name of that path (like 'dogs' or 'cats').