2.py 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. #!/usr/local/bin/python3
  2. import os, sys
  3. import hashlib
  4. import argparse
  5. import humanfriendly
  6. parser = argparse.ArgumentParser(description='What the dupe!?')
  7. optional = parser._action_groups.pop() # Edited this line
  8. required = parser.add_argument_group('required arguments')
  9. optional.add_argument('--threshold', type=str,
  10. help='Only output files greater than \'size\', e.g. 100M')
  11. required.add_argument('--dir', type=str, nargs='?', required=True, action='append',
  12. help='Directory to scan. Can be issued multiple times.')
  13. parser._action_groups.append(optional)
  14. args = parser.parse_args()
  15. sizes = ['10M', '50M', '100M', '1G', '5G', 'gt5GB']
  16. if args.threshold:
  17. bytes = humanfriendly.parse_size(args.threshold)
  18. else:
  19. bytes = 0
  20. def findDup(parentFolder):
  21. # Dups in format {hash:[names]}
  22. dups = {}
  23. print()
  24. for dirName, subdirs, fileList in os.walk(parentFolder):
  25. print(' Scanning %s...' % dirName)
  26. for filename in fileList:
  27. # Get the path to the file
  28. path = os.path.join(dirName, filename)
  29. # Calculate hash
  30. if os.path.exists(path):
  31. # Calculate hash
  32. file_hash = hashfile(path)
  33. # Add or append the file path
  34. if file_hash in dups:
  35. dups[file_hash].append(path)
  36. else:
  37. dups[file_hash] = [path]
  38. return dups
  39. # Joins two dictionaries
  40. def joinDicts(dict1, dict2):
  41. for key in dict2.keys():
  42. if key in dict1:
  43. dict1[key] = dict2[key]
  44. else:
  45. dict1[key] = dict2[key]
  46. def hashfile(path, blocksize = 65536):
  47. afile = open(path, 'rb')
  48. hasher = hashlib.md5()
  49. buf = afile.read(blocksize)
  50. while len(buf) > 0:
  51. hasher.update(buf)
  52. buf = afile.read(blocksize)
  53. afile.close()
  54. return hasher.hexdigest()
  55. def printResults(dict1):
  56. final = {}
  57. for size in sizes:
  58. final[size] = []
  59. del size
  60. print(final)
  61. if bytes > 0:
  62. final[bytes] = []
  63. results = list(filter(lambda x: len(x) > 1, dict1.values()))
  64. for result in results:
  65. file_size = os.path.getsize(result[0])
  66. if bytes > 0:
  67. if file_size >= bytes:
  68. final[bytes].append(result)
  69. else:
  70. #0=10MB 1=50MB 2=100MB 3=1GB 4=5GB
  71. if file_size >= humanfriendly.parse_size(sizes[0]) and file_size < humanfriendly.parse_size(sizes[1]):
  72. final[sizes[1]].append(result)
  73. elif file_size >= humanfriendly.parse_size(sizes[1]) and file_size < humanfriendly.parse_size(sizes[2]):
  74. final[sizes[2]].append(result)
  75. elif file_size >= humanfriendly.parse_size(sizes[2]) and file_size < humanfriendly.parse_size(sizes[3]):
  76. final[sizes[3]].append(result)
  77. elif file_size >= humanfriendly.parse_size(sizes[3]) and file_size < humanfriendly.parse_size(sizes[4]):
  78. final[sizes[4]].append(result)
  79. elif file_size >= humanfriendly.parse_size(sizes[4]):
  80. final[sizes[5]].append(result)
  81. else:
  82. final[sizes[0]].append(result)
  83. if len(results) > 0:
  84. print('___________________')
  85. print('\n\033[1;34m\033[1;34m\u25b6 Duplicates Found\033[0m\n')
  86. print(' The following files are identical. The name could differ, but the content is identical')
  87. print('___________________')
  88. new = ['0']
  89. if bytes > 0:
  90. print("\n\033[1;34m\u25b6 Files bigger than %s\033[0m" % humanfriendly.format_size(bytes, binary=True))
  91. for dupe in final[bytes]:
  92. print('___________________\n')
  93. for file in dupe:
  94. print(' %s' % str(file))
  95. print('___________________')
  96. else:
  97. for size in sizes:
  98. new.append(size)
  99. if len(final[size]) > 0:
  100. if size == 'gt5GB':
  101. print("\n\033[1;34m\u25b6 >= %s\033[0m" % (new[-2]))
  102. else:
  103. print("\n\033[1;34m\u25b6 %s to %s\033[0m" % (new[-2],size))
  104. for dupe in final[size]:
  105. print('___________________\n')
  106. for file in dupe:
  107. print(' %s' % str(file))
  108. print('___________________')
  109. else:
  110. print('No duplicate files found.')
  111. if __name__ == '__main__':
  112. if len(sys.argv) > 1:
  113. dups = {}
  114. folders = args.dir
  115. for i in folders:
  116. # Iterate the folders given
  117. if os.path.exists(i):
  118. # Find the duplicated files and append them to the dups
  119. joinDicts(dups, findDup(i))
  120. else:
  121. print('%s is not a valid path, please verify' % i)
  122. sys.exit()
  123. printResults(dups)
  124. else:
  125. print('Usage: python dupFinder.py folder or python dupFinder.py folder1 folder2 folder3')