Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import os
- import hashlib
- import shutil
- def hash_file(file_path):
- """Generates a hash for the file to uniquely identify it."""
- hasher = hashlib.sha256()
- with open(file_path, 'rb') as f:
- # Read the file in chunks to avoid memory overload with large files
- while chunk := f.read(8192):
- hasher.update(chunk)
- return hasher.hexdigest()
- def find_duplicates(folder):
- """Finds duplicate files in the specified folder based on hash and filesize."""
- files = {}
- duplicates = []
- # Walk through the directory and check each file
- for dirpath, _, filenames in os.walk(folder):
- for filename in filenames:
- file_path = os.path.join(dirpath, filename)
- filesize = os.path.getsize(file_path)
- file_hash = hash_file(file_path)
- # Use a combination of file hash and filesize as the unique key
- key = (file_hash, filesize)
- if key in files:
- duplicates.append(file_path)
- else:
- files[key] = file_path
- return duplicates
- def perform_action(duplicates, action):
- """Performs the specified action on all duplicate files."""
- for duplicate in duplicates:
- if action == 'D':
- os.remove(duplicate)
- print(f"Deleted: {duplicate}")
- elif action == 'M':
- destination = input(f"Enter destination path for {duplicate}: ").strip()
- shutil.move(duplicate, destination)
- print(f"Moved: {duplicate} to {destination}")
- elif action == 'R':
- new_name = input(f"Enter new name (including path) for {duplicate}: ").strip()
- os.rename(duplicate, new_name)
- print(f"Renamed: {duplicate} to {new_name}")
- else:
- print(f"Invalid action for {duplicate}. Skipping.")
- def handle_duplicates(duplicates):
- """Provides the option to handle duplicates either individually or as a batch."""
- print("Duplicates found:")
- for duplicate in duplicates:
- print(duplicate)
- # Ask the user if they want to handle all duplicates with one action
- global_action = input("Would you like to perform the same action for all duplicates? [Y]es or [N]o: ").strip().upper()
- if global_action == 'Y':
- action = input("Choose an action for all files - [D]elete, [M]ove, [R]ename: ").strip().upper()
- perform_action(duplicates, action)
- else:
- # If they prefer to handle files individually, prompt for each file
- for duplicate in duplicates:
- print(f"Duplicate found: {duplicate}")
- action = input("Choose an action for this file - [D]elete, [M]ove, [R]ename: ").strip().upper()
- perform_action([duplicate], action)
- if __name__ == "__main__":
- # Ask the user for the folder to scan for duplicates
- folder_path = input("Enter the folder path to check for duplicates: ").strip()
- # Find duplicates in the provided folder
- duplicates = find_duplicates(folder_path)
- if duplicates:
- print(f"Found {len(duplicates)} duplicate(s).")
- # Let the user choose how to handle the duplicates
- handle_duplicates(duplicates)
- else:
- print("No duplicates found.")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement