码迷,mamicode.com
首页 > 其他好文 > 详细

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

时间:2017-10-24 11:31:16      阅读:159      评论:0      收藏:0      [点我收藏+]

标签:str   深圳   ret   string   open   input   查看   put   查找   

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(草创)
 

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

标签:str   深圳   ret   string   open   input   查看   put   查找   

原文地址:http://www.cnblogs.com/huangbipeng/p/7722091.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!