Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading