Python deletes duplicate files under the same folder

Source: Internet
Author: User

#!/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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.