1

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?

Zoe
  • 27,060
  • 21
  • 118
  • 148

1 Answers1

0

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')
IoaTzimas
  • 10,538
  • 2
  • 13
  • 30
  • Thanks for the suggestion. Im still getting duplicates when I read each iteration. I think I need to place around with the if statement – user12975158 Feb 01 '21 at 00:36
  • Maybe there are some hidden spaces that cause the problem. Try using strip() while checking if ip is inside the file – IoaTzimas Feb 01 '21 at 00:49