2013-03-02 34 views
0

我有一個Python腳本,並在規定順序如下:蟒蛇比較並輸出到新的文件

  1. 需要一個參數(在這種情況下,文件名),並刪除比AZ AZ 0-其他所有字符9和期間'。'。
  2. 剔除了來自不同的是在後面將要進行比較的監視列表IP地址的新文件中的所有信息
  3. 清理文件,並將其保存爲一個新文件,把它比作進行比較,以觀察名單
  4. 最後此清理文件(ip_list_clea)到監視列表文件並將匹配行輸出到新文件(malicious_ips)。

這是第4部分我正在努力。下面的代碼工作,直到第4階段,其中停止休息工作:

#!/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() 

任何想法,爲什麼最後一節不起作用?事實上,它阻止了整個事情的發生。

回答

1

您在上一節中缺少一些冒號。試試這個:

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() 
+0

總是我想念的小事情......非常感謝 – Marq 2013-03-02 01:30:15