python學習筆記——三之修改設定檔

來源:互聯網
上載者:User

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

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.