Advertisement
Manu-J

detect_and_clean_keys

May 23rd, 2024 (edited)
659
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.65 KB | None | 0 0
  1. import os
  2. from colorama import init, Fore, Style
  3.  
  4. # Initialize colorama
  5. init(autoreset=True)
  6.  
  7. def read_keys(file_path):
  8.     with open(file_path, 'r') as file:
  9.         lines = file.readlines()
  10.     return lines
  11.  
  12. def find_duplicates(lines):
  13.     key_count = {}
  14.     duplicates = {}
  15.  
  16.     for i, line in enumerate(lines):
  17.         line = line.strip()
  18.         if line and not line.startswith('#'):
  19.             if line in key_count:
  20.                 key_count[line].append(i)
  21.                 duplicates[line] = key_count[line]
  22.             else:
  23.                 key_count[line] = [i]
  24.    
  25.     print("Key Count:", key_count)
  26.     print("Duplicates:", duplicates)
  27.    
  28.     return duplicates
  29.  
  30. def remove_duplicates(lines, duplicates):
  31.     indices_to_remove = {i for indices in duplicates.values() for i in indices[1:]}
  32.     new_lines = [line for i, line in enumerate(lines) if i not in indices_to_remove]
  33.     return new_lines
  34.  
  35. def main():
  36.     file_path = input("Enter the path to your .keys file: ").strip()
  37.    
  38.     # Read the keys from the file
  39.     lines = read_keys(file_path)
  40.     original_line_count = len(lines)
  41.     original_size = os.path.getsize(file_path)
  42.    
  43.     # Find duplicates
  44.     duplicates = find_duplicates(lines)
  45.  
  46.     # Print duplicates and their positions
  47.     print(Fore.CYAN + Style.BRIGHT + "Duplicates found:")
  48.     if duplicates:
  49.         for key, indices in duplicates.items():
  50.             print(Fore.RED + f"{key}: {len(indices)} times at lines {', '.join(map(str, [i + 1 for i in indices]))}")
  51.  
  52.         # Ask user if they want to remove duplicates
  53.         remove = input(Fore.YELLOW + Style.BRIGHT + "\nDo you want to remove duplicates? (yes/no): ").strip().lower()
  54.         if remove == 'yes':
  55.             # Remove duplicates
  56.             new_lines = remove_duplicates(lines, duplicates)
  57.             new_line_count = len(new_lines)
  58.            
  59.             # Write the cleaned lines back to the file
  60.             with open(file_path, 'w') as file:
  61.                 file.writelines(new_lines)
  62.            
  63.             new_size = os.path.getsize(file_path)
  64.            
  65.             # Print the results
  66.             print(Fore.GREEN + Style.BRIGHT + f"\nOriginal number of lines: {original_line_count}")
  67.             print(Fore.GREEN + Style.BRIGHT + f"New number of lines: {new_line_count}")
  68.             print(Fore.GREEN + Style.BRIGHT + f"\nOriginal file size: {original_size} bytes")
  69.             print(Fore.GREEN + Style.BRIGHT + f"New file size: {new_size} bytes")
  70.         else:
  71.             print(Fore.YELLOW + "No changes made.")
  72.     else:
  73.         print(Fore.GREEN + "No duplicates found.")
  74.  
  75. if __name__ == "__main__":
  76.     main()
  77.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement