I'm trying to write ips which are the response from an api call into a .txt file. So far my code works fine, however I don't want ips that are already in the .txt file to be repeated when I use my program again. Example code below
with open('ips_I.txt','a +') as myfile:
for ip_src in ip_set:
if ip_src + '\n' not in myfile.readlines():
myfile.write(ip_src + '\n')
Output values from api ():
13.27.124.517
12.222.230.4
31.157.283.70
after running the program a second time:
13.27.124.517
65.34.22.212.66
13.27.124.517
0.17.345.137
You can see that value 13.27.124.517 is still repeating in the .txt file. How do I make it so I only append only new ips to the .txt file and not add on any duplicates that already exist it the file?