如何检查第三个文件中是否存在来自两个不同文件的两个不同字符串?

时间:2018-09-19 04:43:03

标签: python python-2.7 python-multithreading

我想检查第三文件中是否存在来自两个不同文件的两个不同字符串,如果存在,则将该行写入第四文件。字符串集是IPv4地址。即使两个文件中都存在字符串,我也会得到空文件。另外,如果可能的话,我想实现多线程/多处理以加快处理速度。非常感谢您提前提供任何建议/帮助。

slave_list文本文件具有如下条目:

hostA 192.168.15.32
hostB 192.168.15.33
hostC 192.168.15.37

static_ip_list文本文件具有如下条目:

192.168.100.10
192.168.100.12
192.168.100.14

slave_logfile的条目如下:

1536043051.176  59320 192.168.100.10 TCP_MISS/200 21830 CONNECT www.google.com:443 - 192.168.15.32

代码:

from datetime import datetime, timedelta
import os
import string
import sys


slave_list = sys.argv[1]
static_ip_list = sys.argv[2]
append_log = open('/home/top10_domain_accessed/logs/append_logs.txt', 'a')
def file_path (slave_list):
  count = 1
  while(count <=30):
  Nth_days = datetime.now() - timedelta(days=count)
  date = Nth_days.strftime("%Y%m%d")
  yr_month = Nth_days.strftime("%Y/%m")
  file_name = 'local2' + '.' + date
  with open(slave_list) as file:
        for line in file:
            string = line.split()
            slave_name = string[0]
            slave_ip = string[1]
            log_path = "/LOGS/%s/%s" %(slave_name, yr_month)
            slave_logfile = os.path.join(log_path, file_name)
            if os.path.exists(slave_logfile):
               log_read = open(slave_logfile, 'r')
               for line in log_read:
                 if slave_ip in line:
                    with open(static_ip_list) as ip_list:
                       for static_ip in ip_list:
                          static_ip = static_ip.rstrip()
                          if static_ip in line:
                             append_log.write(line + '\n')
            else:
             pass

        count = count + 1

if __name__ == '__main__':
     file_path(slave_list)

0 个答案:

没有答案