• python学习笔记——叁之修改配置文件


    简单流程
    循环输出 3个操作选项 读,修改,删除
    输入 1 2 3
    在得到输入的数字后 然后执行相应的方法


    import os

    def read():
    "读取"
    with open("haproxy.txt","r",encoding="utf-8") as r1 :
    list1= r1.readlines()


    for line in list1 :
    if "backend" in line :
    list1_index=list1.index(line)+1
    break

    list2=list1[list1_index:]
    for line_new in list2 :
    print(line_new)

    # def crtate(bakend,server,weight,maxconn):
    def crtate(*args):
    "创建"
    with open("haproxy.txt","a+",encoding="utf_8") as a1:
    arg = {
    'bakend': args[0],
    'record': {
    'server': args[1],
    'weight': args[2],
    'maxconn': args[3]
    }
    }
    print(type(arg))
    for i in arg :
    print(i,arg[i])
    if i=="record":
    a1.write(" "+str(arg.get("record")).replace("'","").replace("{"," ").
    replace("}"," ").replace(":",""))
    else:
    a1.write(" "+str(i))
    a1.write(" "+str(arg[i]))
    a1.flush()

    def delete(x):
    "删除"
    with open("haproxy.txt","r",encoding="utf-8") as d1 ,
    open("haproxy_new.txt","w",encoding="utf-8") as d2 :
    d1a=d1.readlines()
    for index,line in enumerate(d1a):
    if x in line :
    print(index)
    delete_index=index
    print(d1a[delete_index])
    print(d1a[delete_index+1])
    # d1.seek(0)
    d1a.pop(delete_index)
    d1a.pop(delete_index)
    for i in d1a :
    d2.write(i)
    os.rename("haproxy.txt","haproxy.bak")
    os.rename("haproxy_new.txt","haproxy.txt")

    while True:
    q=input("请输入操作编号 1.读取 2.增加 3.删除>>")
    if q=="1":
    read()
    if q=="2":
    w=input("第一个>>>:")
    w1 = input("第二个>>>:")
    w2 = input("第三个>>>:")
    w3 = input("第四个>>>:")
    print(w,w1,w2,w3)
    crtate(w,w1,w2,w3)
    if q=="3":
    s=input("输入一个参数>>>")
    delete(s)
  • 相关阅读:
    java poi 从服务器下载模板写入数据再导出
    一个华为面试题
    ForEach 循环
    fmt标签格式化数字和时间
    Ichars制作数据统计图
    jQuery中的事件
    oracle学习第四天
    GET请求和POST请求
    Jsp的九个隐含对象
    Oracle学习【语句查询】
  • 原文地址:https://www.cnblogs.com/HL-blog/p/7362362.html
Copyright © 2020-2023  润新知