Duplicate File Remover – Remove Duplicate files recursively – Python Code

Duplicate File remover – python code SEARCH Duplicate Files in your computer folders and remove them. Duplicate files is one such problem that almost all the people follow. We make duplicate copies of our files and later on not able to recognize where they are stored and this way they eat-up a lots of our disk-space.

Duplicate File Remover python code

It is really a hard work to locate such duplicate files and delete each one of them manually.

Though There are a lots of Identical file remover programs available out there but they are not working as per the users requirement. They use to display result and then you have to select each file that you want to delete and this is a real pain.

Python is well known for automating boring stuff and it is also known for automating operating system for such thing.

Duplicate file remover program search, files available in your source folder in to the destination folder and if the program is able to find out any such files, it removes all these duplicate files from destination folder.

Python Program to remove duplicate files

# purpose       : To search and delete duplicate files
# name          : rakesh kumar
# licence       : MIT
import os
import sys          #module for terminating 
import glob
import tkinter as tk
from tkinter import filedialog
count=0
root = tk.Tk()
root.withdraw()
sourcedir = filedialog.askdirectory()  #source folder
os.chdir(sourcedir)
source_files = glob.glob('*.*')
targetdir = filedialog.askdirectory()  # target folder
if not targetdir.strip() :
    print ( "Folder Error...User did not selected any folder" )
    #rootdir = 'C:\\Users\\ace
    sys.exit()
for root, subFolders, files in os.walk(targetdir):
         os.chdir(root)
         files = glob.glob('*.*')
         for filename in files:
            if filename in source_files:
                os.unlink(filename)
                count+=1
print ( "Total {} files deleted" .format(count)  )

How to run Duplicate file remover

  1. Copy above Duplicate file remover program in your Python IDE and run this program from your IDE
  2. Select the source folder – Whose files you think has duplicate entries in your computer drives
  3. Select Destination folder / Target folder and that’ it.

Final Notes on duplicate file remover

Above program is able to delete all the occurrence of files available in source folder and deletes in Target folder. This program has a little limitation as this is not able to compare image files. It only compare the names of file If you have any such solution that is able to compare image/sound files, then please let us know, we will update this code with full credits.

 

Leave a Reply