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
names['sun4.jpg'] += 1, you have even special dictionarycollections.Counter()to count. Or you can add to list and only checkif 'sun4.jpg' in names: do_something() else: names.append('sun4.jpg')