python操作文件(增、删、改、查)

时间:2023-03-09 18:43:29
python操作文件(增、删、改、查)

内容

global
log 127.0.0.1 local2
daemon
maxconn 256
log 127.0.0.1 local2 info
defaults
log global
mode http
timeout connect 5000ms
timeout client 50000ms
timeout server 50000ms
option dontlognull listen stats :8888
stats enable
stats uri /admin
stats auth admin:1234 frontend oldboy.org
bind 0.0.0.0:80
option httplog
option httpclose
option forwardfor
log global
acl www hdr_reg(host) -i www.oldboy.org
use_backend www.oldboy.org if www backend www.oldboy1.org
server 10.10.0.10 10.10.0.10 weight 9999 maxconn 33333333333
server 10.10.10.1 10.10.10.1 weight 22 maxconn 2000
server 2.2.2.4 2.2.2.4 weight 20 maxconn 3000
backend www.oldboy2.org
server 3.3.3.3 3.3.3.3 weight 20 maxconn 3000
backend www.oldboy20.org
server 10.10.0.10 10.10.0.10 weight 9999 maxconn 33333333333

 arg = {'backend': 'www.oldboy.org','record':{'server':'10.10.0.10 10.10.0.10',"weight":"", "maxconn": ""}}
count=0
while True:
choice=eval(input("请输入一个字典{}"))
with open( "haproxy.conf",'r') as a,open("b.txt",'w') as b:
for line in a:
count+=1
if count==26:
b.write (str(choice["backend"]))
b.write('\n')
b.write(str(choice["record"]).strip('{}'))
else:b.write(line)

删除:

import sys, time
count=0
l=[]
while True:
with open( "haproxy.conf",'r') as a:
for i in a:
count+=1
print(i,count)
else:
count=0
print(count)
choice=input("请输入要删除的行---->:").strip()
for i in choice:
if i.isdigit():
l.append(int(i))
with open( "haproxy.conf",'r') as a,open("b.txt",'w') as b:
for line in a:
count+=1
if count in l:
continue
else:
b.write(line)
for i in l:
sys.stdout.write("\r ##########正在删除第%s行#########" % i)
sys.stdout.flush()
time.sleep(1)
exit(print('\n 再见!'))
arg = {'backend': 'www.oldboy.org','record':{'server':'10.10.0.10 10.10.0.10',"weight":"9999", "maxconn": "33333333333"}}

while True:
choice = eval(input("请输入一个字典{}:").strip())
with open( "haproxy.conf",'r') as a,open("b.txt",'w') as b:
for line in a:
if line.startswith("backend") and choice["backend"] in line:
continue
if choice["record"]['server'] in line:
continue
else:
b.write(line)

删2:

  

count=0
while True:
with open( "haproxy.conf",'r') as a:
for i in a:
count+=1
print(i,count)
else:
count=0
print(count)
choice=int(input("请输入删除的行:"))
print(choice)
with open( "haproxy.conf",'r') as a,open("b.txt",'w') as b:
for line in a:
count+=1
if count==choice:
continue
else:
b.write(line)

count=
while True:
choice=eval(input("请输入一个字典{}")) # choice=eval(input("请输入一个字典{}"))
with open( "haproxy.conf",'r') as a,open("b.txt",'w') as b:
for line in a:
count+=
if count==:
b.write(str(choice["backend"]).strip())
continue
if count==:
b.write("\n")
continue
if count==:
for k,v in dict(choice['record']).items():
b.write(' ')
b.write(k)
b.write(" ")
b.write(v)
continue
if count==:
b.write("\n")
continue
else:
b.write(line)

 while True:
l=[]
m=input("请输入您要查询的内容: ").strip()
flag=False
with open('haproxy.conf','r') as ha1:
for line in ha1:
if line.startswith("backend") and m in line: #第一步 定位到 backend行
flag=True
continue
if flag==True: #第二步 设置标志开关
l.append(line)
if line.startswith("backend") and flag: #第三步 跳出循环
break
for i in l:
print(i)

用户登录三次锁定:

count=0
while True:
user=input("请输入您的用户名: ").strip()
with open("lock_account",'r') as a:
for i in a:
if user==i.strip():
exit("您的账户已被锁定")
password = input("请输入密码: ").strip()
count+=1
flag = False
with open("account.txt", 'r') as b:
for line in b:
use,psw = line.strip().split()
if user== use and password==psw:
count=0
print("---------欢迎登录-------- ") if count==3 and flag==False:
with open("lock_account", 'a') as a:
a.write(user)
a.write('\n')
exit("输入次数超过3次,你的账户被锁定了")

定义函数精简版

count=0
def open_lockaccount(user):
with open("lock_account",'r') as a:
for i in a:
if user==i.strip():
exit("您的账户已被锁定")
def open_account(user,passwd):
with open("account.txt", 'r') as b:
for line in b:
use,psw = line.strip().split()
if user== use and password==psw:
count = 0
print("---------欢迎登录-------- ")
def write_account(user):
with open("lock_account", 'a') as a:
a.write(user)
a.write('\n')
exit("输入次数超过3次,你的账户被锁定了")
while True:
user=input("请输入您的用户名: ").strip()
open_lockaccount(user)
password = input("请输入密码: ").strip()
count+=1
flag = False
open_account(user, password)
if count == 3 and flag == False:
write_account(user)