#! usr/bin/env python3 # find duplicates of a file specified by the user, # in a directory specified by the user import os, sys, filecmp print("Enter location of the file of which you want to find duplicates of:") location = input() if not os.path.isfile(location): print("Enter the location of a file!") sys.exit() fileName, fileExtension = os.path.splitext(location) print("In which directory do you want to search for duplicate files?") myLocation = input() if not os.path.isdir(myLocation): print("Enter the address of a directory!") sys.exit() for folderName, subfolders, filenames in os.walk(myLocation): for filename in filenames: if filename.endswith(fileExtension): if filecmp.cmp(location, os.path.join(folderName,filename)): print("FILES " + location + " and " + filename + " seem equal") print("Do you want to delete " + os.path.join(folderName,filename) + " ? (y/n)") choice = input() if choice == "y": os.unlink(filename) continue else: continue
Comments about the code are welcome.