Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 859929
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T08:44:37+00:00 2026-05-15T08:44:37+00:00

I have a file of IP addresses called IPs. When I parse a new

  • 0

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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T08:44:38+00:00Added an answer on May 15, 2026 at 8:44 am
    iplist = []
    
    # With takes care of all the fun file handling stuff (closing, etc.)
    with open('ips.txt', 'r') as f:
        for line in f:
            iplist.append(line.strip())   # Gets rid of the newlines at the end
    
    # Change the above to this for Python versions < 2.6
    f = open('ips.txt', 'r')
    for line in f:
        iplist.append(line.strip())
    f.close()
    
    newip = '192.168.1.2'
    
    if newip not in iplist:
        f = open('ips.txt', 'a') # append mode, please
        f.write(newip+'\n')
    

    Now you have your IPs in a list (iplist) and you can easily add your newip to it iplist.append(newip) or do anything else you please.


    Edit:

    Some excellent books for learning Python:
    If you’re worried about programming being difficult, there’s a book that’s geared towards kids, but I honestly found it both easy-to-digest and informative.
    Snake Wrangling for Kids

    Another great resource for learning Python is How to Think Like a Computer Scientist.

    There’s also the tutorial on the official Python website. It’s a little dry compared to the previous ones.

    Alan Gauld, one of the foremost contributors to the tutor@python.org mailing list has this tutorial that’s really good and also is adapted to Python 3. He also includes some other languages for comparison.

    If you want a good dead-tree book, I’ve heard that Core Python Programming by Wesley Chun is a really good resource. He also contributes to the python tutor list every so often.

    The tutor list is another good place to learn about python – reading, replying, and asking your own questions. I actually learned most of my python by trying to answer as many of the questions I could. I’d seriously recommend subscribing to the tutor list if you want to learn Python.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this file 'gardens.php' , which pulls data from a table called 'generalinfo'
I have a properties file which currently contains system names and ip addresses. What
I have tnsnames.ora file like DB_CONNECTION1= (description= (address= (protocol=tcp) (host=myhost1.mydomain.com) (port=1234) ) (connect_data= (sid=ABCD)
I have an xml file that has an address node that looks like <address>
I have a text file with up to 100 IP addresses, 1 per line.
We have a Delphi COM component being called from an ISAPI web app. The
I have a line like this BR_MALLOC %p File - %s, Func - %s,
I have a big form This form is processed by a PHP file called
I have JSON data which contents Strings and Image URLs, and I already parse
I have a .htaccess file that looks like this: SetEnv APPLICATION_ENV development RewriteEngine On

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.