#!/usr/bin/env python
#coding=utf-8
def read_regular_list(path):
regular_list = list()
with open(path, 'r') as file_handle:
for row in file_handle:
regular_list.append(row.strip())
return regular_list
def filte_del_file(from_file, to_file, del_list):
with open(from_file, 'r') as file_handle_from:
with open(to_file, 'w') as file_handle_to:
for row in file_handle_from:
if not any(key_word in row for key_word in del_list):
file_handle_to.write(row)
def filte_file_save(from_file, to_file, save_list):
with open(from_file,'r') as file_handle_from:
with open(to_file,'w') as file_handle_to:
for row in file_handle_from:
if any(key_word in row for key_word in save_list):
file_handle_to.write(row)
if __name__ == '__main__':
del_list = read_regular_list(r"del.txt") #读取过滤规则
filte_del_file(r"user.log", "output.txt", del_list)#过滤文件
save_list = read_regular_list(r"save.txt") #读取过滤规则
filte_file_save(r'user.log',"saveOutput.txt",save_list)#过滤文件
#coding=utf-8
def read_regular_list(path):
regular_list = list()
with open(path, 'r') as file_handle:
for row in file_handle:
regular_list.append(row.strip())
return regular_list
def filte_del_file(from_file, to_file, del_list):
with open(from_file, 'r') as file_handle_from:
with open(to_file, 'w') as file_handle_to:
for row in file_handle_from:
if not any(key_word in row for key_word in del_list):
file_handle_to.write(row)
def filte_file_save(from_file, to_file, save_list):
with open(from_file,'r') as file_handle_from:
with open(to_file,'w') as file_handle_to:
for row in file_handle_from:
if any(key_word in row for key_word in save_list):
file_handle_to.write(row)
if __name__ == '__main__':
del_list = read_regular_list(r"del.txt") #读取过滤规则
filte_del_file(r"user.log", "output.txt", del_list)#过滤文件
save_list = read_regular_list(r"save.txt") #读取过滤规则
filte_file_save(r'user.log',"saveOutput.txt",save_list)#过滤文件