python删除多个文件内的指定内容

import os
datapath = r'F:\mine\data'      #待处理的文件存放目录
files = os.listdir(datapath)
for file in files:
    with open(r'F:\mine\data'+"\\"+file) as f:    #依次打开文件
        file_object = open(os.path.join(datapath, file)).readlines()  #按行读取
    with open(r'F:\mine\out\data.log', 'a+') as w:   #处理后输出的文件目录 处理后的文件内容写在同一log内 注意写的方式是a+  可追可写
            for line in file_object:                 
                if "dis lo"in line:                  #“”内为删除的字段所在的行
                    continue
                elif "down" in line:
                    continue
                elif "Logging buffer " in line:
                    continue
                elif "Allowed max" in line:
                    continue
                elif "Actual buffer size" in line:
                    continue
                elif "Channel number " in line:
                    continue
                elif "Dropped " in line:
                    continue
                elif "Overwritten " in line:
                    continue
                elif "Current messages " in line:
                    continue
                w.write(line)
                f.close()

你可能感兴趣的:(python删除多个文件内的指定内容)