zoukankan      html  css  js  c++  java
  • python3 文件增删查


    #Author by Andy
    #_*_ coding:utf-8 _*_
    import sys
    import time
    Path="E:my python study\fileshaproxy.txt"
    Path_new="E:my python study\fileshaproxy_new.txt"
    examples='''33[31;1m内容格式范例 :
    arg={
    'backend': 'www.oldgirl.org',
    'record':{
    'server': '100.1.7.10',
    'weight': 20,
    'maxconn': 30
    }
    }
    33[;0m'''
    #定义主函数
    def main_func():
    print("Welcome to use this program!")
    choice=input("See what you can do: "
    "Add "
    "Delete "
    "Q or q to end program. "
    "Search "
    ":")
    return choice
    #定义字符串转字典函数
    def str_to_dict():
    print("请输入要添加的内容:")
    stop_input = ''
    user_input = ''
    for line in iter(input,''):
    user_input = user_input+line + ' ' #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(' backend %s ' %backend_value)
    f.write(' server %s weight %s maxconn %s '
    %(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=' server %s weight %s maxconn %s '
    %(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
                }
            }
  • 相关阅读:
    BZOJ2821 作诗(Poetize) 【分块】
    BZOJ2724 蒲公英 【分块】
    Codeforces 17E Palisection 【Manacher】
    BZOJ2565 最长双回文串 【Manacher】
    Codeforces 25E Test 【Hash】
    CODEVS3013 单词背诵 【Hash】【MAP】
    HDU2825 Wireless Password 【AC自动机】【状压DP】
    HDU2896 病毒侵袭 【AC自动机】
    HDU3065 病毒侵袭持续中【AC自动机】
    HDU2222 Keywords Search 【AC自动机】
  • 原文地址:https://www.cnblogs.com/pythonstudy/p/6160197.html
Copyright © 2011-2022 走看看