Python实现检测文件的MD5值来查找重复文件

来源:互联网 发布:word 矩阵对齐 编辑:程序博客网 时间:2024/05/22 03:14

平时学生交上机作业的时候经常有人相互复制,直接改文件名了事,为了能够简单的检测这种作弊行为,想到了检测文件的MD5值,虽然对于抄袭来说作用不大,但是聊胜于无,以后可以做一个复杂点的。

# coding: utf8import hashlibimport osfrom collections import Counterimport sysreload(sys)sys.setdefaultencoding('utf-8')def get_md5_01(file_path):    md5 = None    if os.path.isfile(file_path):        f = open(file_path,'rb')        md5_obj = hashlib.md5()        md5_obj.update(f.read())        hash_code = md5_obj.hexdigest()        f.close()        md5 = str(hash_code).lower()    return md5def get_md5_02(file_path):    f = open(file_path,'rb')      md5_obj = hashlib.md5()    while True:        d = f.read(8096)        if not d:            break        md5_obj.update(d)    hash_code = md5_obj.hexdigest()    f.close()    md5 = str(hash_code).lower()    return md5if __name__ == "__main__":    output_list=[]    #input_path=r"e:\xx\新建文件夹"    #output_path = unicode(input_path , "utf8")    output_path=os.getcwd()    g = os.walk(output_path)      for path,dir_list,file_list in g:          for file_name in file_list:            output_list.append(os.path.join(path, file_name) )    md5_list= [get_md5_01(i) for i in output_list]    Counter_list=Counter(md5_list)    for i in Counter_list.items():        if i[1] >1:            duplicate_list=[ a for a in range(len(md5_list)) if md5_list[a] == i[0]]            print '-'*50            print i[0]            for j in duplicate_list:                with open('duplicate.log', mode='a+') as f:                    f.write(i[0]+'\t'+output_list[j]+'\n')                print output_list[j]
原创粉丝点击