'Append api responses to a .txt file without duplicates [duplicate]

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?



Solution 1:[1]

You currently read the file once, before the loop and any other ip added inside the loop will be considered as 'not included' in the file and will be added again, which is not what you want. You must read the file in each iteration instead. Try the following:

for ip_src in ip_set:
    with open('ips_I.txt','a +') as myfile:
        if ip_src + '\n' not in myfile.readlines():
            myfile.write(ip_src + '\n')

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 IoaTzimas