python小功能 文件夹中保留文件中含有某些字段的行

要求:

# -- coding: utf-8 --
import os

rootdir = ‘d:/a’
savedir = ‘d:/b’

list = os.listdir(rootdir)
for i in range(0,len(list)):
path = os.path.join(rootdir,list[i]).replace(‘\’,’/’)
if os.path.isfile(path):
#读取文件
with open(path,”r”) as f:
lines = f.readlines()
#写文件
with open(path,”w”) as f_w:
for line in lines:
if “there is problem” not in line:
continue
f_w.write(line)

f.close()
f_w.close()

你可能感兴趣的:(python)