1

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

6
  • You can use dictionary to count ie. names['sun4.jpg'] += 1, you have even special dictionary collections.Counter() to count. Or you can add to list and only check if 'sun4.jpg' in names: do_something() else: names.append('sun4.jpg') Commented Jan 10, 2017 at 17:47
  • you can't have two files with the same name in one folder. Commented Jan 10, 2017 at 17:53
  • @furas and I dont, if you read the post correctly you see in the first code box the actual name files. they dont repeat, but if we strip them, we get the last code box, which has repeated names, and if that happens i want to save those names to a txt Commented Jan 10, 2017 at 17:56
  • if you need full names then keep them with short names as a pair. Commented Jan 10, 2017 at 17:57
  • @furas I dont need the full names. Ok lets try to explain it differently, the point is: I have sun4xxxxxx.jpg and sun4yyyyyy.jpg in the same folder. And my goal is to check if the name sun4 repeats it self, if it does, save it Commented Jan 10, 2017 at 17:59

3 Answers 3

2

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")
Sign up to request clarification or add additional context in comments.

1 Comment

thank you, first script work just the way I wanted, although I had to change a bit to keep updating listdir because the folder is allways getting new images. Thank you once again :)
1
import os
MyList=[]
for file in os.listdir("eyeclosed"):
    if file.endswith(".jpg"):
    newstr = file.replace("_face_lefteyeclahe_closed", "")
    newstr = newstr.replace("_face_righteyeclahe_closed", "")
        print(newstr)
        MyList.append(newstr)

#MyList =['glasses3.jpg','good1.jpg','good1.jpg','sun3.jpg','sun4.jpg','vsun4.jpg','sun5.jpg','sun5.jpg']
my_dict = {i:MyList.count(i) for i in MyList}
print my_dict

output

  {'sun5.jpg': 2, 'sun3.jpg': 1, 'good1.jpg': 2, 'glasses3.jpg': 1, 'sun4.jpg': 1,  'vsun4.jpg': 1}

2 Comments

Did exacly what I wanted, short and simple :D Thank you!
Just needs to save to text if any name is count twice
0

You can use split method and check if a file name repeats itself:

import os
present_files = [] # This will contain the unique file names
for filename in os.listdir("eyeclosed"):
    if filename.endswith(".jpg")
        lookname = filename.split('_')[0] #This is the part of the name you are looking for to repeat itself
        if lookname in present_files:
            print(lookname)
        else:
            present_files.append(lookname+".jpg") #".jpg" is optional

You can use split method at your convenience. I don't think there is really any need to replace the elements and then append them. The script will print out the names of the repeating directories. Use the following command to save the output to the file:

python scriptName.py > /tmp/fileCheck.txt

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.