码迷,mamicode.com
首页 > 编程语言 > 详细

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

时间:2017-08-15 10:18:04      阅读:160      评论:0      收藏:0      [点我收藏+]

标签:read   import   record   流程   创建   第一个   apr   python   let   

简单流程
循环输出 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("\n\t\t"+str(arg.get("record")).replace("‘","").replace("{"," ").
replace("}"," ").replace(":",""))
else:
a1.write("\n"+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)

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

标签:read   import   record   流程   创建   第一个   apr   python   let   

原文地址:http://www.cnblogs.com/HL-blog/p/7362362.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!