#!/usr/bin/env python#coding: utf-8import md5import osfrom time import CLOCK AS NOWDEF GETMD5 (filename): file_txt = open (filename, ' RB '). Read () m = md5.new (file_txt) return M.hexdigest () Def main (): path = u ' C:\\aa\bb\cc\dd ' all_md5 = {} all_size = {} total_file = 0 total_delete = 0 start = now ( ) for file in os.listdir (path): total_file += 1 real_path = Os.path.join (Path, file) if os.path.isfile (Real_path) == True: size = os.stat (Real_path) .st_size name_and_md5 = [real_path, ' ] if size in all_ Size.keys (): NEW_MD5 = GETMD5 (Real_path) if all_size[size][1] == ': all_size[size][1] = GETMD5 (all_size[size][0]) if new_md5 in all_size[size]: total_delete += 1 print u ' Delete ', file try: os.remove (Os.path.join (path, file)) except: print ' no such file: %s ' % file else: &nbSp; all_size[size].append (NEW_MD5) else: all_size[size] = name_and_md5 end = now () time_last = end - Start print u ' Total Files: ', total_file print u ' Delete Number: ', total_delete print u ' time: ', time_last, ' seconds ' if __name__ == ' __main__ ': main ()
Reference: http://developer.51cto.com/art/201205/334378.htm
The original text did not perform the delete file operation:
Try:os.remove (Os.path.join (path, file)) Except:print ' No such file:%s '% file
Or look at the official documents.
Https://docs.python.org/2/library/os.html?highlight=os.remove#os.remove
Python deletes duplicate files under the same folder