Python Learning Notes--Modify the configuration file

Source: Internet
Author: User

Simple process
Loop output 3 operation options read, modify, delete
Input 1 2 3
After you have entered the number and then execute the appropriate method


Import OS

def read ():
Read
WithOpen"Haproxy.txt","R",encoding="Utf-8")As R1:
list1= R1.readlines ()


For lineIn List1:
If"Backend"In line:
List1_index=list1.index (line) +1
Break

List2=list1[list1_index:]
For Line_newIn List2:
Print (line_new)

# def crtate (bakend,server,weight,maxconn):
def crtate (*args):
Create
WithOpen"Haproxy.txt","A +",encoding="Utf_8")As A1:
arg = {
' Bakend ': args[0],
' Record ': {
' Server ': args[1],
' Weight ': args[2],
' Maxconn ': args[3]
}
}
PrintType (ARG))
For IIn 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):
Delete
WithOpen"Haproxy.txt","R",encoding="Utf-8")As D1, \
Open"Haproxy_new.txt","W",encoding="Utf-8")As D2:
D1a=d1.readlines ()
For Index,lineInchEnumerate (D1A):
If XIn 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 IIn D1A:
D2.write (i)
Os.rename ("Haproxy.txt","Haproxy.bak")
Os.rename ("Haproxy_new.txt","Haproxy.txt")

While True:
q=InputPlease enter the action number 1. Read 2. Add 3. Delete >> ")
If q=="1":
Read ()
if q== "2":
W =input ( "first >>>:")
W1 = input ( "second >>>:")
W2 = Span style= "COLOR: #000080" >input ( "third >>>:")
W3 = Span style= "COLOR: #000080" >input ( "fourth >>>:")
print (W,W1,W2,W3)
Crtate (W,W1,W2,W3)
if q== "3":
S =input ( "Enter a parameter >>>")
Delete (s)

Python Learning Notes--Modify the configuration file by three

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.