# takes lines without duplicates from one file and outputs them in another # Useful for filters, hosts, etc. import os seen = set() filename = './hosts.txt' # f filename_out = './hosts_new.txt' # fout try: with open(filename, 'r+') as f, open('./hosts_new.txt', 'w') as fout: lines = f.readlines() if not lines: print("The file is empty.") exit(0) print(f"Read {len(lines)} lines from the file.") f.seek(0) f.truncate() for line in lines: line_lower = line.lower().strip() # convert line, strip whitespace - is it necessary? if line_lower not in seen: fout.write(line) seen.add(line_lower) print(f"Wrote {len(seen)} unique lines to fout.") except FileNotFoundError: print(f"File not found: {filename}") except PermissionError: print(f"Permission denied. Please run the script as administrator/root.") except Exception as e: print(f"An error occurred: {e}")