Python uses MD5 file verification to find files with duplicate content

From , 3 Years ago, written in Python, viewed 103 times.
URL https://pastebin.vip/view/01064f1d
  1. #!/usr/bin/python
  2. #encoding=utf-8
  3.  
  4. #import the modules
  5. import os
  6. import os.path
  7. import sys
  8. import hashlib
  9.  
  10. #define the functions
  11. def findFile(rootPath, fileSeq, delSeq):
  12.     dirs = os.listdir(rootPath)                                                         #list the directories under the root path
  13.     for dir in dirs:                                                                            #traversal all the directories
  14.         path = rootPath + os.sep + dir                                          #complete the path of current file
  15.         if os.path.isdir(path):
  16.             findFile(path, fileSeq, delSeq)                                     #if current file is a directory, recursive the function
  17.         else:
  18.             md5Check(path, fileSeq, delSeq)                                     #if not a directory, check the md5
  19.  
  20. def md5Check(path, fileSeq, delSeq):
  21.     f = file(path, 'rb')                                                                        #open the file with 'read-only' and 'binary'
  22.     md5obj = hashlib.md5()
  23.     md5obj.update(f.read())                                                                     #calculate the md5
  24.     if md5obj.hexdigest() in fileSeq:
  25.         delSeq.append(path)                                                                     #if md5 of current file is in the fileSeq, put the file path into the delSeq
  26.     else:
  27.         fileSeq.append(md5obj.hexdigest())                                      #if not in the fileSeq, put the md5 into the fileSeq
  28.     f.close()                                                                                           #close the file
  29.  
  30. def delList(delSeq):
  31.     print 'These files are waiting to be removed:'
  32.     for delFile in delSeq:
  33.         print delFile                                                                           #list the file path in the delSeq
  34.  
  35. #the main program
  36. fileSeq = []
  37. delSeq = []
  38. while True:
  39.     if len(sys.argv) == 1:
  40.         rootPath = raw_input('Enter the root path: ')           #one parameter means no parameter, ask the root path
  41.     else:
  42.         rootPath = sys.argv[1]                                                          #or get the second parameter as the root path
  43.     try:
  44.         findFile(rootPath, fileSeq, delSeq)                                     #try if the root path is valid
  45.     except(OSError):
  46.         print 'The root path is invalid. Please enter again. '
  47.         del sys.argv[1:]
  48.         continue                                                                                        #catch the except and delete all invalid parameters
  49.     break
  50.  
  51. if len(delSeq) == 0 :
  52.     print 'No duplicate file was found! '                                       #if no files in delSeq, exit
  53. else:
  54.     delList(delSeq)                                                                                     #or list the delSeq
  55.     while True:
  56.         answer = raw_input('Would you want to remove these files? Please answer yes(y) or no(n): ')
  57.         answer.lower
  58.         if answer in ('yes', 'y'):                                                      #if "yes"
  59.             for delFile in delSeq:
  60.                 try:
  61.                     os.remove(delFile)                                          #remove all files in delSeq
  62.                 except(OSError):
  63.                     print 'Warning! "%s" is not existed! ' % delFile
  64.                     continue                                                            #ignore the files witch are not existed
  65.             print 'All duplicate files have been removed! '
  66.             break
  67.         elif answer in ('no', 'n'):
  68.             print 'Process has exited without any change! '
  69.             break                                                                                       #if "no", do nothing
  70.         else:
  71.             print 'Please enter yes(y) or no(n). '
  72. sys.exit()                                                                                                      #exit
  73. #//python/7142

Reply to "Python uses MD5 file verification to find files with duplicate content"

Here you can reply to the paste above

captcha

https://burned.cc - Burn After Reading Website