Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import os
- from colorama import init, Fore, Style
- # Initialize colorama
- init(autoreset=True)
- def read_keys(file_path):
- with open(file_path, 'r') as file:
- lines = file.readlines()
- return lines
- def find_duplicates(lines):
- key_count = {}
- duplicates = {}
- for i, line in enumerate(lines):
- line = line.strip()
- if line and not line.startswith('#'):
- if line in key_count:
- key_count[line].append(i)
- duplicates[line] = key_count[line]
- else:
- key_count[line] = [i]
- print("Key Count:", key_count)
- print("Duplicates:", duplicates)
- return duplicates
- def remove_duplicates(lines, duplicates):
- indices_to_remove = {i for indices in duplicates.values() for i in indices[1:]}
- new_lines = [line for i, line in enumerate(lines) if i not in indices_to_remove]
- return new_lines
- def main():
- file_path = input("Enter the path to your .keys file: ").strip()
- # Read the keys from the file
- lines = read_keys(file_path)
- original_line_count = len(lines)
- original_size = os.path.getsize(file_path)
- # Find duplicates
- duplicates = find_duplicates(lines)
- # Print duplicates and their positions
- print(Fore.CYAN + Style.BRIGHT + "Duplicates found:")
- if duplicates:
- for key, indices in duplicates.items():
- print(Fore.RED + f"{key}: {len(indices)} times at lines {', '.join(map(str, [i + 1 for i in indices]))}")
- # Ask user if they want to remove duplicates
- remove = input(Fore.YELLOW + Style.BRIGHT + "\nDo you want to remove duplicates? (yes/no): ").strip().lower()
- if remove == 'yes':
- # Remove duplicates
- new_lines = remove_duplicates(lines, duplicates)
- new_line_count = len(new_lines)
- # Write the cleaned lines back to the file
- with open(file_path, 'w') as file:
- file.writelines(new_lines)
- new_size = os.path.getsize(file_path)
- # Print the results
- print(Fore.GREEN + Style.BRIGHT + f"\nOriginal number of lines: {original_line_count}")
- print(Fore.GREEN + Style.BRIGHT + f"New number of lines: {new_line_count}")
- print(Fore.GREEN + Style.BRIGHT + f"\nOriginal file size: {original_size} bytes")
- print(Fore.GREEN + Style.BRIGHT + f"New file size: {new_size} bytes")
- else:
- print(Fore.YELLOW + "No changes made.")
- else:
- print(Fore.GREEN + "No duplicates found.")
- if __name__ == "__main__":
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement