用了函数实现文件的增、删、改、查、但是有很多重复的代码,需要再改进。目前先实现基本功能 Punch import time,os oprate_list={'add':'add','delete':'delete','modify':'modify','check':'check'} #使用字典 定义选项 match_word='backend' #匹配的第一个关键字 match_server='server' #匹配的第二个关键字, 确保记录在一个区域内,所以匹配两个关键字 def menu(): #定义一个菜单,打印出来让用户查看 for key,value in oprate_list.items(): #循环打印出键、值 print('"{key_1}":{value_1}'.format(key_1=key,value_1=value)) def oprate_(): #定义操作流程, while True: print('\n-------file oprate--------') time.sleep(0) menu() time.sleep(0) choice=input('input option::') if choice == oprate_list['check']: #匹配用户的选项,如果正确则进入该函数,调用相应功能 time.sleep(0) check_() if choice == oprate_list['delete']: delete() if choice == oprate_list['modify']: modify() if choice == oprate_list['add']: add() def add(): #文件内容增加功能: '''file add oprate''' domain=input(' example:www.oldboy1.org\ninpute domain:') with open('hello.txt') as ha_del_one, open('hellotwo.txt', 'w+') as file_read: #打开两个文件,在代码中不能直接修改文件,只能通过一个文件中转实现功能 for line in ha_del_one: if line.strip().startswith(match_word) and domain in line: #与用户交互,匹配相应信息 使用join 方法 修改文件 add_server_ip =input('example: 10.10.0.10 10.10.0.10\n input server IP record: ').strip() add_weight = input('example: 20 \ninput weight IP record: ') maxconnone = input('maxconn recommend config 100,\n input maxconn record: ').strip() line = "".join([line.strip(),'\n{space_val} server {server_ip} weight {weight_val} maxconn {maxconn_val}\n']).format(space_val=(' '*7),server_ip=add_server_ip,weight_val=add_weight,maxconn_val=maxconnone) file_read.write(line) os.rename('hello.txt', 'hellobak.txt') #将源文件更名, os.rename('hellotwo.txt', 'hello.txt') #将修改过的文件更名为源文件名,这样用户的感受就是在操作一个文件 os.remove('hellobak.txt') #并删除源文件,这样看上去就是一个文件了 print('write info done!') def delete(): #定义删除功能 """del file contents""" flag=True choice=input('example:www.oldboy1.org\ninput delete domain: ').strip() # 匹配用户输入的信息 time.sleep(0.5) if flag==False: print("input invalid") with open('test.txt',) as ha_del,open('testdel.txt','w') as ha_del_one: #同样的,删除功能在代码中实现方法也是打两个文件,读出一个文件的内容,将不需要的continue,写到另一个去,这种给用户感觉就是在删除了某一条记录 for line in ha_del: if line.strip().startswith(match_word) and choice in line: server_IP = input('example: 10.10.0.10 10.10.0.10\n input delete server IP: ').strip() #匹配信息 time.sleep(0.5) print('deleting {delete_serverIP}'.format(delete_serverIP=server_IP)) if line.strip().startswith(match_server) and server_IP in line: continue flag = True if flag: ha_del_one.write(line) #写入信息 #os.rename('test.txt','test.txt') with open('testdel.txt') as ha_del_two: for line_read in ha_del_two: if line_read.strip().startswith('backend') and choice not in line_read: flag=True continue print(line_read.strip()) #打印出修改过的信息 def modify(): #定义修改文件函数 '''file modify oprate''' with open('hello.txt') as check_mod: #打开操作的文件,循环打印出所有的信息,并标上行号 a=1 for line in check_mod: print(a,' '*2,line.strip()) a+=1 flag=False domain=input(' example:www.oldboy1.org\ninpute domain:') #匹配用户想修改的信息 with open('hello.txt') as ha_del_one, open('hellotwo.txt', 'w+') as file_read: #打开两个文件,一个源文件,一个是空文件,也就另外再创建一个 (number=a,info=red_list.strip())) ha_del_one.seek(0) for line in ha_del_one: if line.strip().startswith(match_word) and domain in line: flag=True old_serip = input('input old server ip: ').strip() #用户输入旧的server ip old_weight = input('inpute old weight value: ') #用户输入旧的weight 值 if line.strip().startswith(match_server) and old_serip and old_weight in line: new_server_ip=input('example: 5 \n input new server IP record: ').strip() #输入新的server ip new_weight_val = input('example: 5 \n input new weight record: ').strip() #输入新的weight 值 line=line.replace(old_serip,new_server_ip) # 新的server ip 替换 旧 server ip line=line.replace(old_weight, new_weight_val) #新的weight 值 替换 旧的 weight 值 if flag: file_read.write(line) def check_(): """look function """ check_li=[] flag=False domain=input(' example:www.oldboy1.org\ninpute domain:') #让用户输入 ,匹配用户想查看的 信息 with open('test.txt') as ha: for line in ha: if line.strip().startswith(match_word) and domain in line: #如果用户 输入的信息在文件中先跳过,然后打印出其中的内容 flag=True continue if line.strip().startswith(match_word) and flag: break if flag: check_li.append(line) #添加到一个列表,下一步直接循环列表,打印出信息 a=1 for red_list in check_li: #打印出读取出来的信息,并加上行号,有多少条信息 print('-------infomation---------\n{number}:::: {info}'.format(number=a,info=red_list.strip())) a+=1 time.sleep(2) if __name__ == '__main__': # oprate_() # check_(domain_input) oprate_() #只需要调用操作函数就可以让用户选择增、删、改、查功能。 并调用相应的功能函数
转载于:https://www.cnblogs.com/tonycloud/articles/6682799.html