第三节课作业(文件修改在,增加,查看)

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了第三节课作业(文件修改在,增加,查看)相关的知识,希望对你有一定的参考价值。

import sys
def lookup(): ##查看
    with open(‘haproxy‘,‘r+‘,encoding=‘utf-8‘) as f:
        line = input(‘Please enter the lookup domain name:‘)
        for linef in f:
            if line in linef:
                print(linef)
                print(f.readline())
            else:
                continue
def dell(): #删除前先查找
    with open(‘haproxy‘, ‘r+‘, encoding=‘utf-8‘) as f:
        delle = input("Please enter the domain name to delete:")
        for linef in f:
            if delle in linef:
                return linef,f.readline()
            else:
                continue

def increase(bakend,server,weight,maxconn,record=‘record‘):
    out = ‘‘‘
bakend  %s
        server %s weight %s maxconn %s
    ‘‘‘ % (bakend,server,weight,maxconn)
    return (out)

def incres(): ##增加
    bakend = input("Please enter the domain name:")
    with open(‘haproxy‘,‘r+‘,encoding=‘utf-8‘) as f:
        for line in f:
            if bakend in line:
                print("Domain name already exists.")
                sys.exit()
            else:
                continue
    server = input("Please enter the server IP address:")
    weigth = input("Please input weight:")
    maxconn = input("Please input timeout time:")
    x = increase(bakend,server,weigth,maxconn)
    with open(‘haproxy‘,‘a‘,encoding=‘utf-8‘) as f:
        f.write(x)
def dellet():
    with open(‘haproxy‘, ‘r+‘, encoding=‘utf-8‘) as f , open(‘haproxy_1‘,‘w‘,encoding=‘utf-8‘) as f_1 :
        x = dell()
        for line in f:
             if line in x:
                 continue
             else:
                 f_1.write(line)

inp = input("Please input what you want to do(Add,delete,view):")
if inp == ‘add‘:
    incres()
elif inp == ‘delete‘:
    dellet()
elif inp == ‘view‘:
    lookup()
else:
    exit()


深圳运维开发交流群 --->>599706738(草创)
 

以上是关于第三节课作业(文件修改在,增加,查看)的主要内容,如果未能解决你的问题,请参考以下文章

第三节课作业150206309

第三节课作业150206309

第三节课作业150206309

第三节课作业150206309

第三节课作业150206309

第三节课作业150206309