Working on a small script that allows the user to selected a folder to search for duplicate files, whether that be images, text etc. It should then move those duplicate files into another folder of the users choice.
This is the code i have so far:
from tkinter import Tk
from tkinter.filedialog import askdirectory
import os
import shutil
import hashlib
Tk().withdraw()
source = askdirectory(title="Select the source folder")
walker = os.walk(source)
uniqueFiles = dict()
total = 0
for folder, sub_folder, files in walker:
for file in files:
filepath = os.path.join(folder, file)
filehash = hashlib.md5((open(filepath, "rb").read())).hexdigest()
if filehash in uniqueFiles:
print(f"{filepath} is a duplicate")
total += 1
else:
uniqueFiles[filehash] = source
print("\n# of duplicate files found: {} ".format(total))
# destination = askdirectory(title="Select the target folder")
# shutil.move(filepath, destination, copy_function=shutil.copytree)
It works perfectly fine for now, finding all the duplicate files in a folder/sub folders and printing them out. The part im stuck in is how to move them. the commented code at the bottom seems to work but it prompts the user for a folder for every duplicate found. I just want it to list out all the duplicates and then move them at once.
Any ideas on how i could format my code?
Thanks!