Sep-08-2022, 05:54 PM
(Sep-08-2022, 11:45 AM)DeaD_EyE Wrote: This makes the code shorter:
from ipaddress import IPv4Address, ip_address def make_ipv4_set(file_like): results = set() for line in map(str.strip, file_like): try: addr = ip_address(line) except ValueError: continue if isinstance(addr, IPv4Address): results.add(addr) return results with ( open("ip1_file.txt") as ip1_file, open("ip2_file.txt") as ip2_file, ): ip1_set = make_ipv4_set(ip1_file) ip2_set = make_ipv4_set(ip2_file) # sorting works because IPv4Address is sortable results = sorted(ip1_set - ip2_set) # results is now a list. sorted returns always a list # iterate over the elements and print them for ip in results: # converting IPv4Address to str print(str(ip))
I entered this IP44.44.44.44.11 on ip2_file.txt and when I ran the program it doesn't print out this IP address could it be because we are treading the IP as IPv4? can we just assign all the IP as text or number and than compare and extract which isn't in the first file ip1_file.txt