我想检查一个文件名是否在同一个文件夹上重复。如果我能说明我的情况就更好了
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
这是指图像上的双眼(左右)。我想知道两个眼睛是否都闭着,也就是说,如果两个图像名称重复它们本身,两个眼睛都闭着(例如:sun4) 让我们简化:
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)
给了我们:
glasses3.jpg
good1.jpg
good1.jpg
sun3.jpg
sun4.jpg
sun4.jpg
sun5.jpg
sun5.jpg
好的,现在我的目标是知道哪些名字重复它们自己,如果是这样,把它保存到一个txt文件。例如,sun4重复它自己,这意味着两只眼睛都闭上了,所以保存到一个txt文件
sun4.jpg both eyes closed
有人知道如何检查文件是否重复?谢谢你
输出
因为您只需要检查哪个(短)名称重复,所以可以使用list来记住以前的名称,并检查此列表中是否存在下一个名称。
顺便说一句:如果您需要这个名称重复多少次,那么您可以使用dictionary来计算它或
collections.Counter()
。可以使用split方法检查文件名是否重复:
你可以在方便的时候使用分割法。我不认为真的需要替换元素然后附加它们。脚本将打印出重复目录的名称。使用以下命令将输出保存到文件:
相关问题 更多 >
编程相关推荐