Finding new IP in a file

Posted by Gary on Stack Overflow See other posts from Stack Overflow or by Gary
Published on 2010-06-10T18:10:57Z Indexed on 2010/06/10 18:12 UTC
Read the original article Hit count: 473

Filed under:
|
|

Hello. I have a file of IP addresses called "IPs". When I parse a new IP from my logs, I'd like to see if the new IP is already in file IPs, before I add it. I know how to add the new IP to the file, but I'm having trouble seeing if the new IP is already in the file.

!/usr/bin/python
from IPy import IP
IP = IP('192.168.1.2')

f=open(IP('IPs', 'r')) #This line doesn't work

f=open('IPs', 'r') # this one doesn't work
for line in f:
if IP == line:
print "Found " +IP +" before"
f.close()

In the file "IPs", each IP address is on it's own line. As such:
222.111.222.111
222.111.222.112
Also tried to put the file IPs in to an array, but not having good luck with that either. Any ideas?
Thank you,
Gary

© Stack Overflow or respective owner

Related posts about compare

Related posts about ip