# coding=utf-8
import csv
import os
#搜索项文件
res = r'key.txt'
#目标文件
paths = r'tab/'
#搜索项
list_key =[]
# 获取项目搜索项
def get_re(file_dir):
re = csv.reader(open(file_dir))
for dct in re:
list_key.append(dct[0])
return re
get_re(file_dir=res)
#便利需要搜索的csv文件
def list_dir(file_dir):
dir_list = os.listdir(file_dir)
for cur_file in dir_list:
path = os.path.join(file_dir,cur_file)
if os.path.isfile(path):
dir_files = os.path.join(file_dir, cur_file)
if os.path.splitext(path)[1] == '.csv':
csv_file = os.path.join(file_dir, cur_file)
list_csv.append(csv_file)
if os.path.isdir(path):
list_dir(path)
return list_csv
#处理
def main(list_csv):
for data in list_csv:
#写入的文件
fo = open(os.path.splitext(data)[0] + '.txt', "w+")
#数据源
reader = csv.reader(open(data))
num = 0
for line in reader:
tmp = 'true'
num = num+1
if len(line)>0:
Name = line[0]
for k in list_key:
if k in Name:
tmp = 'false'
print(['相关sql:'+Name, ';\n'])
fo.writelines([Name, ';\n'])
print([num,tmp, ';\n'])
fo.close()
list_csv = []
list_dir(file_dir=paths)
main(list_csv = list_csv)