import configparser
import os
import LineScan
cf = configparser.ConfigParser()
cf.read("Config.ini",encoding='utf8')
file_Types = (cf.get("FileTypeFilter","FileTypes")).split(',')
fileFolder = (cf.get("FileTypeFilter","FileFolder"))
write_log= open('log.txt', 'w',encoding='utf8')
write_log.write("搜索的路径为:"+fileFolder+"\n")
#遍历给定的文件夹及子文件夹,获取所有的文件
def eachFile(file_path):
    file_List = []
    for (root, dirs, files) in os.walk(file_path):
        for fileName in files:
            file_List.append(os.path.join(root,fileName))
    return file_List
for file_Type in file_Types:
    file_List = eachFile(fileFolder)
    #过滤当前类型的文件
    file_List = [x for x in file_List if os.path.splitext(x)[1]==file_Type]
    write_log.write("==========现在读取的文件类型是:"+file_Type+"\n")
    if len(file_List) != 0:
        #write_log.write("==========包含的文件有:"+str(file_List)+"\n")
        for file in file_List:
           
            write_log.write("==========当前读取的文件是:"+file+"\n")
            a=0
            with open(file, 'r',encoding='utf8') as f:
                for line in f.readlines():
                    a=a+1
                    if LineScan.KeyWordFilter(line):
                        write_log.write("文件:"+file+" 第"+str(a)+"行有可疑目标,请检查\n")
                        write_log.write("这一行是:"+line+"\n")                      
                      
    else:
        write_log.write("==========没有"+ file_Type+"文件==========\n")
write_log.close()
posted on 2018-02-02 16:41  怡星梦  阅读(468)  评论(0编辑  收藏  举报