Don't say anything, directly on the source
#!/usr/bin/env python#Coding=utf-8ImportOSImportMD5Import Timedefgetmd5 (filename):" "Formal Parameters: File name return: MD5 Code for files" "file= open (filename,'RB') File_content= File.read (1024*1024) File.close () m=md5.new (file_content)returnm.hexdigest ()defDelfile (flist_temp):" "formal parameter: File name list returns: The file name to delete list" "dellist=[] flist=[] fsize= [] forFinchflist_temp:ifOs.path.isfile (f): Fsize.append (Os.stat (f). St_size) Flist.append (f) forIinchRange (len (fsize)): forJinchRange (i+1, Len (fsize)):ifFsize[i] = =Fsize[j]:ifGETMD5 (flist[i]) = =getmd5 (Flist[j]): Dellist.append (Flist[i]) Break returndellistdefMain ():" "Delete duplicate files in the current directory (including duplicate files under subfolders)" " Print 'Delete duplicate files in the current directory (including duplicate files under subfolders) \ n'Start=time.clock () path=os.getcwd () list_fn= [] forIinchOs.walk (path): forFninchI[-1]: Full_path=Os.path.join (I[0],FN) list_fn.append (Full_path) list_fn.reverse ()Print 'Total files: \ t', Len (LIST_FN),'\ n'delf=delfile (LIST_FN) forFinchdelf:Print 'Delete \ t', F#Os.remove (f)End =Time.clock ()Print '\ n Total files: \ t', Len (LIST_FN),'\ n' Print 'Delete file: \ t', Len (delf),'\ n' Print 'total time: \ t', End-start,'\ n' #os.remove (' delrefile.py ')Time.sleep (30) return0if __name__=='__main__': Main ()
View Code
Results:
Thank you.
Reference: Delete directory under the same file, step-by-step optimization (Python implementation) http://www.cnblogs.com/ma6174/archive/2012/05/05/2484415.html
Python Delete duplicate file attachment source code