04-09,Work-对文件的增删改查(函数)

#增删改查之查
def look():
while 1 :
enter = input("查看:如www.oldboy.org ")
l = []
flag = False
with open("haproxy.conf") as enter_read:
for line in enter_read:
if line.startswith("backend") and enter in line:
flag = True
continue
if line.startswith("backend") and flag:
break
if flag:
l.append(line.strip())
for out in l:
print(out)
#增删改查之删
def delete():
dels = input("删除:如www.oldboy.org ")
with open("haproxy.conf") as dels_read,open("text",mode="w") as dels_del:
for line in dels_read:
dels_del.write(line)
if line.startswith("backend") and dels in line:
break
#增删改查之改
def amend():
modify = input("修改:如www.oldboy.org ")
servers = " server 1111 weight 2222 maxconn 3333"
with open("haproxy.conf") as modify_read,open("text",mode="w") as modify_m:
for line in modify_read:
modify_m.write(line)
if line.startswith("backend") and modify in line:
modify_m.write(servers)
break
#增删改查之增
def add():
agg = input("增加:如www.oldboy.org ")
servers = " server 1111 weight 2222 maxconn 3333 "
with open("haproxy.conf") as agg_read, open("text", mode="w") as agg_a:
for line in agg_read:
agg_a.write(line)
if line.startswith("backend") and agg in line:
agg_a.write(servers)

#列表
def list():
user_list = '''
1.look 查看:
2.delete 删除:
3.amend 修改:
4.add 增加:
0.exit 退出:
'''
print(user_list)

def mains():
list()
judge = input("弄什么:")
if judge==1:
look()
if judge==2:
delete()
if judge==3:
amend()
if judge==4:
add()
if judge==0:
exit()
mains()
原文地址:https://www.cnblogs.com/gz369521/p/6686028.html