i created a program to compare two text file , and identify duplicate and unique items but first for loop is running only once after that it exit without iterating second item in the file. if any one can help please do.
f1 = open("file1.txt","r")
f2 = open("file2.txt","r")
duplicate = open("duplicate_ip.txt", "w")
unique = open("unique_ip.txt", "w")
for x in f1:
for y in f2:
if x == y:
duplicate.write(y)
else:
unique.write(x)
file1.txt contains following
192.168.1.1
192.168.10.2
192.168.56.5
192.16.10.2
192.168.5.5
file2.txt contain following
192.168.1.2
10.10.10.0
10.10.10.11
192.168.11.111
127.0.0.1
172.16.31.5
CodePudding user response:
If you want f1 and f2 to be list of strings, then please use readlines()
method. Also don't forget to close files (at least the ones you write to).
f1 = open("file1.txt", "r").readlines()
f2 = open("file2.txt", "r").readlines()
duplicate = open("duplicate_ip.txt", "w")
unique = open("unique_ip.txt", "w")
for x in f1:
for y in f2:
if x == y:
duplicate.write(y)
else:
unique.write(x)
f1.close()
f2.close()
duplicate.close()
unique.close()
But there is a much simple way to manage file IO sessions with use of context manager. Your code will then will be looking something like this
with open("file1.txt", "r") as f1, \
open("file2.txt", "r") as f2, \
open("duplicate_ip.txt", "w") as duplicate, \
open("unique_ip.txt", "w") as unique:
f1_lines = f1.readlines()
f2_lines = f2.readlines()
for x in f1_lines:
for y in f2_lines:
if x == y:
duplicate.write(y)
else:
unique.write(x)
CodePudding user response:
Solution with set operation & (intersection) and ^ (XOR operation)
f1_ip = set(open("file1.txt","r"))
f2_ip = set(open("file2.txt","r"))
with open("duplicate_ip.txt", "w") as duplicate:
for ip in f1_ip & f2_ip:
duplicate.write(ip)
with open("unique_ip.txt", "w") as unique:
for ip in f1_ip ^ f2_ip:
unique.write(ip)
CodePudding user response:
you can write like this:
with open("file1.txt", "r") as f1:
data1 = set()
for line in f1.readlines():
data1.add(line.strip('\n'))
with open("file2.txt", "r") as f2:
data2 = set()
for line in f2.readlines():
data2.add(line.strip('\n'))
unique_list = data1.difference(data2)
duplicate_list = data1.intersection(data2)
with open("duplicate_ip.txt", "w") as duplicate:
for ip in duplicate_list:
duplicate.write(ip "\n")
with open("unique_ip.txt", "w") as unique:
for ip in unique_list:
unique.write(ip "\n")
CodePudding user response:
To answer the question, you're opening the file, but not reading the lines, so you're not actually iterating over the file.
Couple comments:
- you should try to open files using
with
so you don't accidentily leave them open - this still wont work, as the last line won't have a \n so you should probably remove them using
.replace
- you're only checking one way, any ips in file2 that aren't in file 1 wont be found this way, not sure if that's what you want
- there are faster ways to check which items are the same in two lists and which are unique
To solve the main issue:
with open("file1.txt","r") as f1:
data1 = f1.readlines()
with open("file2.txt","r") as f2:
data2 = f2.readlines()
with open("duplicate_ip.txt", "w") as duplicate,
open("unique_ip.txt", "w") as unique:
for x in data1:
for y in data2:
if x == y:
duplicate.write(y)
else:
unique.write(x)