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

  • SEARCH
  • Home
  • 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 8313111
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T20:23:04+00:00 2026-06-08T20:23:04+00:00

I am trying to get a script working, where it will check the existance

  • 0

I am trying to get a script working, where it will check the existance of an IP in a lookup csv file, and then if it exists take the third element and remove that third element from another (second) file. Here is a extract of what I have:

for line in fileinput.input(hostsURLFileLoc,inplace =1):
        elements = open(hostsLookFileLoc, 'r').read().split(".").split("\n")
        first = elements[0].strip()
        third = elements[2].strip()
        if first == hostIP:
                if line != third:
                        print line.strip()

This obviously doesn’t work, I have tried playing with a few options, but here is my latest (crazy) attempt.

I think the problem is that there are two input files open at once.

Any thoughts welcome,

Cheers

  • 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-06-08T20:23:05+00:00Added an answer on June 8, 2026 at 8:23 pm

    All right, even though I haven’t got any response to my comment on the question, here’s my shot at a general answer. If I’ve got something wrong, just say so and I’ll edit to try to address the errors.

    First, here are my assumptions. You have two files, who’s names are stored in the HostsLookFileLoc and HostsURLFileLoc variables.

    The file at HostsLookFileLoc is a CSV file, with an IP address in the third column of each row. Something like this:

    HostsLookFile.csv:

    blah,blah,192.168.1.1,whatever,stuff
    spam,spam,82.94.164.162,eggs,spam
    me,myself,127.0.0.1,and,I
    ...
    

    The file at HostsURLFileLoc is a flat text file with one IP address per line, like so:

    HostsURLFile.txt:

    10.1.1.2
    10.1.1.3
    10.1.2.253
    127.0.0.1
    8.8.8.8
    192.168.1.22
    82.94.164.162
    64.34.119.12
    ...
    

    Your goal is to read and then rewrite the HostsURLFile.txt file, excluding all of the IP addresses that are found in the third column of a row in the CSV file. In the example lists above, localhost (127.0.0.1) and python.org (82.94.164.162) would be excluded, but the rest of the IPs in the list would remain.

    Here’s how I’d do it, in three steps:

    1. Read in the CSV file and parse it using the csv module to find the IP addresses. Stick them into a set.
    2. Open the flat file and read the IP addresses into a list, closing the file afterwards.
    3. Reopen the flat file and overwrite it with the loaded list of addresses, skipping any that are contained in the set from the first step.

    Code:

    import csv
    
    def cleanURLFile(HostsLookFileLoc, HostsURLFileLoc):
        """
        Remove IP addresses from file at HostsURLFileLoc if they are in
        the third column of the file at HostsLookFileLoc.
        """
        with open(HostsLookFileLoc, "r") as hostsLookFile:
            reader = csv.reader(hostsLookFile)
            ipsToExclude = set(line[2].strip() for line in reader)
    
        with open(HostsURLFileLoc, "r") as hostsURLFile:
            ipList = [line.strip() for line in hostsURLFile]
    
        with open(HostsURLFileLoc, "w") as hostsURLFile: # truncates the file!
            hostsURLFile.write("\n".join(ip for ip in ipList
                                         if ip not in ipsToExclude))
    

    This code is deliberately simple. There are a few things that could be improved, if they are important to your use case:

    • If something crashes the program during the rewriting step, HostsURLFile.txt may be clobbered. A safer way of rewriting (at least, on Unix-style systems) is to write to a temp file, then after the writing has finished (and the file has been closed), rename the temp file over the top of the old file. That way if the program crashes, you’ll still have the original version or a completely written replacement, but never anything in between.
    • If the checking you needed to do was more complicated than set membership, I’d add an extra step between 2 and 3 to do the actual processing, then write the results out without further manipulation (other than adding newlines).
    • Speaking of newlines, if you have a trailing newline, it will be passed through as an empty string in the list of IP addresses, which should be OK for this scenario (it won’t be in the set of IPs to exclude, unless your CSV file has a messed up line), but might cause trouble if you were doing something more complicated.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm pulling my hair out trying to get a jQuery script working that will
I am trying to get my session login script working and I had the
I have a script that I'm trying to get working. Basically, what I'm trying
I'm trying to get a Ruby script to download a file off a server,
So I've been trying to get this script working in IE 7 & 8.
I'm trying to get the code below working so that it will call a
I'm trying to get the php executable to parse scripts, but it's not working.
I am trying to get this script to work. it opens up a directry
I've been trying to get a script that inserts a div of text after
I am currently trying to get a script to write output from other started

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.