首页 > 代码库 > 文件增、删、改、查

文件增、删、改、查

 1 while True:
 2     l=[]
 3     m=input("请输入您要查询的内容: ").strip()
 4     flag=False
 5     with open(haproxy.conf,r) as  ha1:
 6         for line in ha1:
 7             if line.startswith("backend") and m in line:  #第一步 定位到 backend行
 8                 flag=True
 9                 continue
10             if flag==True:                                #第二步 设置标志开关
11                 l.append(line)
12             if line.startswith("backend") and flag:      #第三步 跳出循环
13                 break
14         for i in  l:
15             print(i)

删除:

arg = {backend: www.oldboy.org,record:{server:10.10.0.10 10.10.0.10,"weight":"9999", "maxconn": "33333333333"}}

while True:
    choice = eval(input("请输入一个字典{}:").strip())
    with open( "haproxy.conf",r) as a,open("b.txt",w) as b:
      for line in a:
            if line.startswith("backend") and choice["backend"] in line:
                continue
            if choice["record"][server] in line:
                continue
            else:
                b.write(line)

 

文件增、删、改、查