Search code examples
pythonfilenames

Checking file name with python


I want to check if a file name repeats itself on the same folder. Better if I illustrate my situation

eyeclosed/
├── glasses3_face_righteyeclahe_closed.jpg
├── good1_face_lefteyeclahe_closed.jpg
├── good1_face_righteyeclahe_closed.jpg
├── sun3_face_righteyeclahe_closed.jpg
├── sun4_face_lefteyeclahe_closed.jpg
├── sun4_face_righteyeclahe_closed.jpg
├── sun5_face_lefteyeclahe_closed.jpg

This is referent to both eyes (left and right) on a image. And I want to know if both eyes are closed, that means, if 2 image names repeat them self both eyes are closed ( example: sun4 ) Lets simplify:

import os
for file in os.listdir("eyeclosed"):
    if file.endswith(".jpg"):
    newstr = file.replace("_face_lefteyeclahe_closed", "")
    newstr = newstr.replace("_face_righteyeclahe_closed", "")
        print(newstr)

which give us:

glasses3.jpg
good1.jpg
good1.jpg
sun3.jpg
sun4.jpg
sun4.jpg
sun5.jpg
sun5.jpg    

Ok so now my goal is to know which names repeat themselves and if so save it to a txt file. Example, sun4 repeats itself so that means both eyes are closed, so save to a txt file

sun4.jpg both eyes closed 

Does someone know how to check if the file repeats itself? Thank you


Solution

  • Because you have to check only which (short) name repeats then you can use list to remeber previous names and check if next name exist on this list.

    listdir = [
        'glasses3_face_righteyeclahe_closed.jpg',
        'good1_face_lefteyeclahe_closed.jpg',
        'good1_face_righteyeclahe_closed.jpg',
        'sun3_face_righteyeclahe_closed.jpg',
        'sun4_face_lefteyeclahe_closed.jpg',
        'sun4_face_righteyeclahe_closed.jpg',
        'sun5_face_lefteyeclahe_closed.jpg',
    ]
    
    names = [] # list to remember previous names
    
    for file in listdir:
        if file.endswith(".jpg"):
            newstr = file.replace("_face_lefteyeclahe_closed", "")
            newstr = newstr.replace("_face_righteyeclahe_closed", "")
            # check if new name is already on list
            if newstr in names:
                print(newstr, "both eyes closed")
            else:
                # add new name to list first time
                names.append(newstr)
    

    BTW: If you would need how many times this name repeats then you could use dictionary to count it or collections.Counter().

    listdir = [
        'glasses3_face_righteyeclahe_closed.jpg',
        'good1_face_lefteyeclahe_closed.jpg',
        'good1_face_righteyeclahe_closed.jpg',
        'sun3_face_righteyeclahe_closed.jpg',
        'sun4_face_lefteyeclahe_closed.jpg',
        'sun4_face_righteyeclahe_closed.jpg',
        'sun5_face_lefteyeclahe_closed.jpg',
    ]
    
    import collections
    
    names = collections.Counter()
    
    for file in listdir:
        if file.endswith(".jpg"):
            newstr = file.replace("_face_lefteyeclahe_closed", "")
            newstr = newstr.replace("_face_righteyeclahe_closed", "")
            names.update([newstr])
    
    for name, count in names.items():
        if count > 1:
            print(name, "both eyes closed")