python3 文件增删查


#Author by Andy
#_*_ coding:utf-8 _*_
import sys
import time
Path="E:\my python study\\files\haproxy.txt"
Path_new="E:\my python study\\files\haproxy_new.txt"
examples='''\033[31;1m内容格式范例 :
arg={
'backend': 'www.oldgirl.org',
'record':{
'server': '100.1.7.10',
'weight': 20,
'maxconn': 30
}
}
\033[;0m'''
#定义主函数
def main_func():
print("Welcome to use this program!")
choice=input("See what you can do:\n"
"Add\n"
"Delete\n"
"Q or q to end program.\n"
"Search\n"
":")
return choice
#定义字符串转字典函数
def str_to_dict():
print("请输入要添加的内容:")
stop_input = ''
user_input = ''
for line in iter(input,''):
user_input = user_input+line + '\n' #input遇到空行才停止,默认是遇到回车停止
dict = eval(user_input.strip("arg = "))
return dict
#定义追加函数
def add(info):
backend_value=info['backend']
record_value=info['record']
f=open(Path,'a+',encoding='utf-8')
f.write('\nbackend %s \n' %backend_value)
f.write('\t\tserver %s weight %s maxconn %s\n'
%(record_value['server'],record_value['weight'],record_value['maxconn']))
time.sleep(2)
f.closed
# add(str_to_dict())
#定义删除函数
def dele(info):
backend_value=info['backend']
record_value=info['record']
del_record='\t\tserver %s weight %s maxconn %s\n' \
%(record_value['server'],record_value['weight'],record_value['maxconn'])
f = open(Path,'r+',encoding='utf-8')
f_new=open(Path_new,'w',encoding='utf-8')
for line in f:
if backend_value in line:
line=line.replace(line,'')
elif del_record == line:
line=line.replace(del_record,'')
f_new.write(line)
time.sleep(2)
f.closed
f_new.closed
#dele(str_to_dict())

def find_keywords(keywords):
f=open(Path,'r+',encoding='utf-8')
while True:
line = f.readline()
if keywords in line:
f.seek(f.tell()+1)
print(f.readline().strip())
target=f.readline().strip()
time.sleep(2)
f.closed
return target
while True:
choice=main_func()
if choice=='Add':
print(examples)
add(str_to_dict())
elif choice=='Search':
keywords=input("请输入关键字:")
find_keywords(keywords)
elif choice=='Delete':
print(examples)
dele(str_to_dict())
elif choice=='q'or choice =='Q':
print("Bye-bye!")
exit()




文件格式:
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.oldboy1.org
use_backend www.oldboy1.org if www

backend www.oldboy.org
server 100.1.7.9 100.1.7.9 weight 20 maxconn 3000

需求:
1、查
    输入:www.oldboy.org
    获取当前backend下的所有记录

2、新建
    输入:
        arg = {
            'bakend': 'www.oldboy.org',
            'record':{
                'server': '100.1.7.9',
                'weight': 20,
                'maxconn': 30
            }
        }

3、删除
    输入:
        arg = {
            'bakend': 'www.oldboy.org',
            'record':{
                'server': '100.1.7.9',
                'weight': 20,
                'maxconn': 30
            }
        }
posted @ 2016-12-11 18:52  想自由  阅读(568)  评论(0编辑  收藏  举报