Advertisement
Guest User

Untitled

a guest
Mar 28th, 2017
47
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.25 KB | None | 0 0
  1. import hashlib
  2. import sys
  3. import os
  4. import glob
  5. from os.path import isfile, join
  6.  
  7. def sha256(fname):
  8. hash = hashlib.sha256()
  9. with open(fname, "rb") as f:
  10. for chunk in iter(lambda: f.read(4096), b""):
  11. hash.update(chunk)
  12. return hash.hexdigest()
  13.  
  14. def get_files(path):
  15. return [file for file in glob.glob(path + '/**/*.*', recursive=True)]
  16.  
  17. def main(args):
  18. hashes = {}
  19. files = get_files(args[1])
  20. should_delete = []
  21.  
  22. # .sort() sorts by filename, whatsapp stores files in yyyy-mm-dd format
  23. # so sorting alphabetically is fine.
  24. files.sort()
  25.  
  26. # Sort by modified date (when pic was taken I think)
  27. # files.sort(key=lambda x: os.path.getmtime(x), reverse=False)
  28.  
  29. for file in files:
  30. checksum = sha256(file)
  31.  
  32. if checksum in hashes:
  33. should_delete.append(file)
  34. else:
  35. hashes[checksum] = file
  36.  
  37. print("Duplicate files:\n")
  38.  
  39. for file in should_delete:
  40. print(file)
  41.  
  42. yesno = input("Delete these {} files? y/n\n".format(len(should_delete)))
  43.  
  44. if yesno.lower().startswith('y'):
  45. for file in should_delete:
  46. print("Deleting '{}'".format(file))
  47. os.remove(file)
  48.  
  49. print('Done.')
  50.  
  51. print('Exiting.')
  52. exit(0)
  53.  
  54. if __name__ == "__main__":
  55. if len(sys.argv) != 2:
  56. print('Invalid argument')
  57. print('Usage: python3 {} directory'.format(__file__))
  58. exit(1)
  59.  
  60. main(sys.argv)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement