import os
import glob #可用通配符匹配文件和文件夹
import filecmp #用于对文件和目录进行比较
#删除aaa文件夹里面所有重复的文件(大小,内容,文件名相同)
dir_path = r'C:\Users\Administrator\Desktop\aaa' #路径,需修改
file_lst = []
for i in glob.glob(dir_path + '/**/*', recursive=True): #recursive=True开启递归
if os.path.isfile(i): #判断是否是文件
file_lst.append(i) #如果是文件则将绝对路径存放到列表中
#遍历上面的路径列表
for x in file_lst:
for y in file_lst:
if x != y and os.path.exists(x) and os.path.exists(y): #如果文件存在
if filecmp.cmp(x, y): #进行文件比较
os.remove(y) #进行文件删除