使用python删除txt文件中两行之间的内容

txt=open(r'test.txt')
lines=[]
flag=False                                       # 立flag
for line in txt:
    if line.strip()=="start line":        
        flag=True                                # 起始行开始不再添加
    if not flag:
        lines.append(line)                    # 需求之外的行添加到列表
    if line.strip()=="end line":
        flag=False                               # 结束行开始恢复添加
txt.close()
open(r'test.txt','w').writelines(lines)

猜你喜欢

转载自www.cnblogs.com/wangzhilong/p/11933808.html