代码适用情况:
xml文件,循环出现某几行,根据这几行中的某个字段删掉这几行
这段代码的作用删除jenkins中config.xml中在自动生成pipline报错的时的回滚
start = '<se.diabol.jenkins.pipeline.DeliveryPipelineView plugin=' end = '</se.diabol.jenkins.pipeline.DeliveryPipelineView>' def delete_pip(src): f = open(src, 'r') lines = []; pip = [] flag = True for line in f: if start in line: pip = [True] flag = False if flag: lines.append(line) else: pip.append(line) if pipline_name in line: pip[0] = False if end in line: flag = True if pip[0]: for i in pip[1:]: lines.append(i) f.close() f = open(src, 'w') f.writelines(lines) f.close()