Marq
Marq

Reputation: 47

python compare and output to new file

I have a Python script that does the following in the stated order:

  1. Takes an argument (in this case a filename) and removes all characters other than a-z A-Z 0-9 and period '.'.
  2. Strips out all information from the new file except IP addresses that are later going to be compared to a watchlist
  3. Cleans up the file and saves it as a new file to be compared to the watchlist
  4. finally it compares this cleaned up file (ip_list_clea) to the watchlist file and outputs matching lines to a new file (malicious_ips).

It is part 4 I am struggling with. The following code works up until stage 4 which stops the rest from working:

#!/usr/bin/python
import re
import sys
import cgi


# Compare the cleaned up list of IPs against the botwatch
# list and output the results to a new file.                                                                                        

new_list = set()
outfile = open("final_downloads/malicious_ips", "w")
for line in open("final_downloads/ip_list_clean", "r")
    if line in open("/var/www/botwatch.txt", "r")
        outfile.write(line)
        new_list.add(line)
outfile.close()

Any ideas as to why the last section does not work? In fact, it stops the whole thing from working.

Upvotes: 0

Views: 132

Answers (1)

grc
grc

Reputation: 23595

You are missing some colons in the last section. Try this:

new_list = set()
outfile = open("final_downloads/malicious_ips", "w")
for line in open("final_downloads/ip_list_clean", "r"):
        if line in open("/var/www/botwatch.txt", "r"):
                outfile.write(line)
                new_list.add(line)
outfile.close()

Upvotes: 1

Related Questions