检索指定目录下的MD5值相同的文件

即检查重复文件,用于去重

#!/usr/bin/python3
# 将文件夹内的文件MD5值相同的挑出来存到另一个目录
# 其中目标目录下所有子文件夹及其子文件夹都会被遍历

# 设置模式,开启最后detect_and_report或detect_and_move函数

# 1.首先写目标目录
# 2.detect_and_report,是只输出报告,不会移动文件
# 3.detect_and_move,将一个相同的文件移动到指定的目录,所以还需要指定一个缓存的目录


import os,hashlib,shutil,sys

# 目标目录
target_directory = 'E:\\Music'
# 目标目录

if not os.path.isdir(target_directory):
    print('目标目录不存在,请确定')
    sys.exit()

# 存储相同的文件的目录
cache_directory = 'E:\\Cache'

# 如果没有目录则创建
if not os.path.isdir(cache_directory):
    os.mkdir(cache_directory)


# 将目录下的所有带路径的文件名存入列表
list_path = []

# 将目录下的所有文件计算MD5值并存入列表
list_mdfz = []

# 遍历目录,包括子目录内所有文件输出到list_path
def listdir(path, list_path):
    for file in os.listdir(path):  
        file_path = os.path.join(path, file)  
        if os.path.isdir(file_path):  
            listdir(file_path, list_path)
        else:
            list_path.append(file_path)
listdir(target_directory,list_path)
list_path.reverse()


# 计算MD5值
def return_mdf(file):
    if os.path.isfile(file): 
        file_op = open(file,'rb') 
        contents = file_op.read() 
        file_op.close() 
        reuslt = hashlib.md5(contents).hexdigest()
    else: 
        reuslt = '0'
    return reuslt

# 输出检查后的报告,如果有相同的,生成report.log,并输出两个文件名(带路径)
def detect_report(num,contents):
        file_path = "report.log"
        file = open(file_path,"a",encoding='utf-8')
        file.write("第"+str(num)+"组MD5相同"+'\n')
        file.write("原路径:"+contents.replace(u'\xa0', u'')+'\n')
        file.close

# 输出文件大小
def get_filesize(file):
    fsize = os.path.getsize(file)
    fsize = fsize/float(1024*1024)
    return round(fsize,2)

# 检查目录内是否有MD5相同的文件,如果有则只输出报告
# 下方的函数是检查有则将重复的第二个移动到指定目录当中
def detect_and_report():
    filez = 0
    count = 0
    samen = 0
    total = len(list_path)
    for x in list_path:
        print('总共'+str(total)+'个文件,当前第:'+str(count)+'个')
        print(x)
        print('重复的文件已经有:'+str(int(filez))+'MB')
        gen_mdf = return_mdf(x)
        if gen_mdf in list_mdfz:
            detect_report(samen,list_path[list_mdfz.index(gen_mdf)])
            detect_report(samen,x)
            filez = filez+get_filesize(x)
            samen+=1
        list_mdfz.append(gen_mdf)
        count+=1
detect_and_report()


def move_report(file_name,contents):
        file_path = cache_directory+"\\"+file_name+".txt"
        file = open(file_path,"a")
        file.write("这是这个同名文件的标注信息:"+'\n')
        file.write("原路径:"+contents)
        file.close

# 检查目录内是否有MD5相同的文件,如果有则将第二个文件移动到指定目录,并在目录内生成一个txt报告文件
def detect_and_move():
    for x in list_path:
        the_mdf = return_mdf(x)
        # 如果已经有该文件了,将该文件移动到Cache目录
        if the_mdf in list_mdfz:
            try:
                shutil.move(x,cache_directory)
                move_report(os.path.basename(x),os.path.dirname(x))
            except Exception as e:
                if 'already exists' in str(e):
                    os.remove(x)
                    print('已经删除了更多的重复,目录:'+os.path.dirname(x))
            print('文件重复:'+x)
        else:
            list_mdfz.append(the_mdf)
        pass

# detect_and_move()
Last modification:February 28th, 2021 at 03:39 pm
硬币投入口