Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import hashlib
- import sys
- import os
- import glob
- from os.path import isfile, join
- def sha256(fname):
- hash = hashlib.sha256()
- with open(fname, "rb") as f:
- for chunk in iter(lambda: f.read(4096), b""):
- hash.update(chunk)
- return hash.hexdigest()
- def get_files(path):
- return [file for file in glob.glob(path + '/**/*.*', recursive=True)]
- def main(args):
- hashes = {}
- files = get_files(args[1])
- should_delete = []
- # .sort() sorts by filename, whatsapp stores files in yyyy-mm-dd format
- # so sorting alphabetically is fine.
- files.sort()
- # Sort by modified date (when pic was taken I think)
- # files.sort(key=lambda x: os.path.getmtime(x), reverse=False)
- for file in files:
- checksum = sha256(file)
- if checksum in hashes:
- should_delete.append(file)
- else:
- hashes[checksum] = file
- print("Duplicate files:\n")
- for file in should_delete:
- print(file)
- yesno = input("Delete these {} files? y/n\n".format(len(should_delete)))
- if yesno.lower().startswith('y'):
- for file in should_delete:
- print("Deleting '{}'".format(file))
- os.remove(file)
- print('Done.')
- print('Exiting.')
- exit(0)
- if __name__ == "__main__":
- if len(sys.argv) != 2:
- print('Invalid argument')
- print('Usage: python3 {} directory'.format(__file__))
- exit(1)
- main(sys.argv)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement