標籤: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學習筆記——三之修改設定檔