Advertisement
Guest User

Untitled

a guest
Oct 10th, 2024
175
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.25 KB | None | 0 0
  1. import os
  2. import hashlib
  3. import shutil
  4.  
  5. def hash_file(file_path):
  6.     """Generates a hash for the file to uniquely identify it."""
  7.     hasher = hashlib.sha256()
  8.     with open(file_path, 'rb') as f:
  9.         # Read the file in chunks to avoid memory overload with large files
  10.         while chunk := f.read(8192):
  11.             hasher.update(chunk)
  12.     return hasher.hexdigest()
  13.  
  14. def find_duplicates(folder):
  15.     """Finds duplicate files in the specified folder based on hash and filesize."""
  16.     files = {}
  17.     duplicates = []
  18.  
  19.     # Walk through the directory and check each file
  20.     for dirpath, _, filenames in os.walk(folder):
  21.         for filename in filenames:
  22.             file_path = os.path.join(dirpath, filename)
  23.             filesize = os.path.getsize(file_path)
  24.             file_hash = hash_file(file_path)
  25.  
  26.             # Use a combination of file hash and filesize as the unique key
  27.             key = (file_hash, filesize)
  28.             if key in files:
  29.                 duplicates.append(file_path)
  30.             else:
  31.                 files[key] = file_path
  32.  
  33.     return duplicates
  34.  
  35. def perform_action(duplicates, action):
  36.     """Performs the specified action on all duplicate files."""
  37.     for duplicate in duplicates:
  38.         if action == 'D':
  39.             os.remove(duplicate)
  40.             print(f"Deleted: {duplicate}")
  41.         elif action == 'M':
  42.             destination = input(f"Enter destination path for {duplicate}: ").strip()
  43.             shutil.move(duplicate, destination)
  44.             print(f"Moved: {duplicate} to {destination}")
  45.         elif action == 'R':
  46.             new_name = input(f"Enter new name (including path) for {duplicate}: ").strip()
  47.             os.rename(duplicate, new_name)
  48.             print(f"Renamed: {duplicate} to {new_name}")
  49.         else:
  50.             print(f"Invalid action for {duplicate}. Skipping.")
  51.  
  52. def handle_duplicates(duplicates):
  53.     """Provides the option to handle duplicates either individually or as a batch."""
  54.     print("Duplicates found:")
  55.     for duplicate in duplicates:
  56.         print(duplicate)
  57.  
  58.     # Ask the user if they want to handle all duplicates with one action
  59.     global_action = input("Would you like to perform the same action for all duplicates? [Y]es or [N]o: ").strip().upper()
  60.  
  61.     if global_action == 'Y':
  62.         action = input("Choose an action for all files - [D]elete, [M]ove, [R]ename: ").strip().upper()
  63.         perform_action(duplicates, action)
  64.     else:
  65.         # If they prefer to handle files individually, prompt for each file
  66.         for duplicate in duplicates:
  67.             print(f"Duplicate found: {duplicate}")
  68.             action = input("Choose an action for this file - [D]elete, [M]ove, [R]ename: ").strip().upper()
  69.             perform_action([duplicate], action)
  70.  
  71. if __name__ == "__main__":
  72.     # Ask the user for the folder to scan for duplicates
  73.     folder_path = input("Enter the folder path to check for duplicates: ").strip()
  74.  
  75.     # Find duplicates in the provided folder
  76.     duplicates = find_duplicates(folder_path)
  77.    
  78.     if duplicates:
  79.         print(f"Found {len(duplicates)} duplicate(s).")
  80.         # Let the user choose how to handle the duplicates
  81.         handle_duplicates(duplicates)
  82.     else:
  83.         print("No duplicates found.")
  84.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement