istxt = re.compile(r’.if.',re.I)
for line in file1.readlines():
line = line.strip()
ifstr = re.findall(istxt,line)
if ifstr:
print ifstr
删除特定行(创建新文件,把不含字符的那些行写进新文件中,重命名文件成原来的文件名称)
复制代码
‘’’
import re
import os
file1 = open(‘test.txt’,‘r+’)
new_file = ‘new_test.txt’
if not 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 in file1.readlines():