#!/usr/bin/python
#-*- coding:utf-8 -*-import re
import os
file1= open('test.txt','r+')
new_file= 'new_test.txt'
ifnot os.path.exists(new_file):
os.system(r"touch {}".format(new_file))
file2= open('new_test.txt','r+')
istxt= re.compile(r'.*if.*',re.I)for line infile1.readlines():
line=line.strip()
ifstr=re.findall(istxt,line)ififstr:
line=[]else:
file2.write(line+ '\n')
file1.close()
file2.close()
#重命名文件
os.rename("new_test.txt","test.txt")