|
| 1 | +import os |
| 2 | +import hashlib |
| 3 | +from collections import defaultdict |
| 4 | + |
| 5 | +def get_file_hash(path, chunk_size=8192): |
| 6 | + """Generate SHA256 hash for a file.""" |
| 7 | + h = hashlib.sha256() |
| 8 | + try: |
| 9 | + with open(path, "rb") as f: |
| 10 | + while chunk := f.read(chunk_size): |
| 11 | + h.update(chunk) |
| 12 | + return h.hexdigest() |
| 13 | + except (PermissionError, FileNotFoundError): |
| 14 | + return None |
| 15 | + |
| 16 | +def scan_folder(folder_path, mode="hash"): |
| 17 | + """Scan folder and find duplicates based on mode ('name', 'size', or 'hash').""" |
| 18 | + duplicates = defaultdict(list) |
| 19 | + for root, _, files in os.walk(folder_path): |
| 20 | + for file in files: |
| 21 | + file_path = os.path.join(root, file) |
| 22 | + if mode == "name": |
| 23 | + key = file |
| 24 | + elif mode == "size": |
| 25 | + try: |
| 26 | + key = os.path.getsize(file_path) |
| 27 | + except OSError: |
| 28 | + continue |
| 29 | + elif mode == "hash": |
| 30 | + key = get_file_hash(file_path) |
| 31 | + if not key: |
| 32 | + continue |
| 33 | + else: |
| 34 | + raise ValueError("Mode must be 'name', 'size', or 'hash'.") |
| 35 | + duplicates[key].append(file_path) |
| 36 | + return {k: v for k, v in duplicates.items() if len(v) > 1} |
| 37 | + |
| 38 | +def display_duplicates(duplicates): |
| 39 | + if not duplicates: |
| 40 | + print("✅ No duplicates found.") |
| 41 | + return |
| 42 | + print("\n🔍 Duplicate Files Found:\n") |
| 43 | + for group, files in duplicates.items(): |
| 44 | + print(f"Group ({len(files)} files):") |
| 45 | + for f in files: |
| 46 | + print(" ", f) |
| 47 | + print("-" * 60) |
| 48 | + |
| 49 | +def delete_duplicates(duplicates): |
| 50 | + for group, files in duplicates.items(): |
| 51 | + keep = files[0] |
| 52 | + print(f"\nKeeping: {keep}") |
| 53 | + for f in files[1:]: |
| 54 | + try: |
| 55 | + os.remove(f) |
| 56 | + print(f"🗑 Deleted duplicate: {f}") |
| 57 | + except Exception as e: |
| 58 | + print(f"⚠️ Could not delete {f}: {e}") |
| 59 | + |
| 60 | +if __name__ == "__main__": |
| 61 | + folder = input("Enter folder path to scan: ").strip() |
| 62 | + print("\nSelect comparison mode:") |
| 63 | + print("1. By name\n2. By size\n3. By hash (recommended)") |
| 64 | + mode_choice = input("Enter choice (1/2/3): ").strip() |
| 65 | + mode = {"1": "name", "2": "size", "3": "hash"}.get(mode_choice, "hash") |
| 66 | + |
| 67 | + duplicates = scan_folder(folder, mode) |
| 68 | + display_duplicates(duplicates) |
| 69 | + |
| 70 | + if duplicates: |
| 71 | + action = input("\nDo you want to delete duplicates? (y/n): ").strip().lower() |
| 72 | + if action == "y": |
| 73 | + delete_duplicates(duplicates) |
| 74 | + print("\n✅ Deletion complete.") |
| 75 | + else: |
| 76 | + print("\nNo files were deleted.") |
0 commit comments