zoukankan      html  css  js  c++  java
  • python操作文件(增、删、改、查)

    内容

    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.oldboy.org
            use_backend www.oldboy.org if www
    
    backend www.oldboy1.org
            server 10.10.0.10 10.10.0.10 weight 9999 maxconn 33333333333
            server 10.10.10.1 10.10.10.1 weight 22 maxconn 2000
            server 2.2.2.4 2.2.2.4 weight 20 maxconn 3000
    backend www.oldboy2.org
            server 3.3.3.3 3.3.3.3 weight 20 maxconn 3000
    backend www.oldboy20.org
            server 10.10.0.10 10.10.0.10 weight 9999 maxconn 33333333333

     1 arg = {'backend': 'www.oldboy.org','record':{'server':'10.10.0.10 10.10.0.10',"weight":"9999", "maxconn": "33333333333"}}
     2 count=0
     3 while True:
     4     choice=eval(input("请输入一个字典{}"))
     5     with open( "haproxy.conf",'r') as a,open("b.txt",'w') as b:
     6         for line in a:
     7             count+=1
     8             if count==26:
     9              b.write (str(choice["backend"]))
    10              b.write('
    ')
    11              b.write(str(choice["record"]).strip('{}'))
    12             else:b.write(line)

    删除:

    import sys, time
    count=0
    l=[]
    while True:
        with open( "haproxy.conf",'r') as a:
                for i in a:
                    count+=1
                    print(i,count)
                else:
                    count=0
                    print(count)
        choice=input("请输入要删除的行---->:").strip()
        for i in choice:
            if i.isdigit():
                l.append(int(i))
        with open( "haproxy.conf",'r') as a,open("b.txt",'w') as b:
            for line in  a:
                count+=1
                if count in l:
                    continue
                else:
                    b.write(line)
        for i in l:
            sys.stdout.write("
     ##########正在删除第%s行#########" % i)
            sys.stdout.flush()
            time.sleep(1)
        exit(print('
     再见!'))
    arg = {'backend': 'www.oldboy.org','record':{'server':'10.10.0.10 10.10.0.10',"weight":"9999", "maxconn": "33333333333"}}
    
    while True:
        choice = eval(input("请输入一个字典{}:").strip())
        with open( "haproxy.conf",'r') as a,open("b.txt",'w') as b:
          for line in a:
                if line.startswith("backend") and choice["backend"] in line:
                    continue
                if choice["record"]['server'] in line:
                    continue
                else:
                    b.write(line)

     删2:

      

    count=0
    while True:
        with open( "haproxy.conf",'r') as a:
            for i in a:
                count+=1
                print(i,count)
            else:
                count=0
                print(count)
        choice=int(input("请输入删除的行:"))
        print(choice)
        with open( "haproxy.conf",'r') as a,open("b.txt",'w') as b:
            for line in  a:
                count+=1
                if count==choice:
                    continue
                else:
                    b.write(line)

     改

    count=0
    while True:
        choice=eval(input("请输入一个字典{}")) # choice=eval(input("请输入一个字典{}"))
        with open( "haproxy.conf",'r') as a,open("b.txt",'w') as b:
            for line in a:
                count+=1
                if count==28:
                    b.write(str(choice["backend"]).strip())
                    continue
                if count==29:
                    b.write("
    ")
                    continue
                if count==30:
                    for k,v in dict(choice['record']).items():
                      b.write('    ') 
                      b.write(k)
                      b.write("   ")
                      b.write(v)
                    continue
                if count==31:
                    b.write("
    ")
                    continue
                else:
                    b.write(line)

     1 while True:
     2     l=[]
     3     m=input("请输入您要查询的内容: ").strip()
     4     flag=False
     5     with open('haproxy.conf','r') as  ha1:
     6         for line in ha1:
     7             if line.startswith("backend") and m in line:  #第一步 定位到 backend行
     8                 flag=True
     9                 continue
    10             if flag==True:                                #第二步 设置标志开关
    11                 l.append(line)
    12             if line.startswith("backend") and flag:      #第三步 跳出循环
    13                 break
    14         for i in  l:
    15             print(i)

     用户登录三次锁定:

    count=0
    while True:
        user=input("请输入您的用户名: ").strip()
        with open("lock_account",'r') as a:
            for i in  a:
               if user==i.strip():
                    exit("您的账户已被锁定")
        password = input("请输入密码: ").strip()
        count+=1
        flag = False
        with open("account.txt", 'r') as b:
            for line in b:
               use,psw = line.strip().split()
               if user== use and password==psw:
                   count=0
                   print("---------欢迎登录-------- ")
    
        if count==3 and flag==False:
            with open("lock_account", 'a') as a:
                a.write(user)
                a.write('
    ')
                exit("输入次数超过3次,你的账户被锁定了")

     定义函数精简版

    count=0
    def open_lockaccount(user):
        with open("lock_account",'r') as a:
            for i in  a:
               if user==i.strip():
                    exit("您的账户已被锁定")
    def open_account(user,passwd):
        with open("account.txt", 'r') as b:
            for line in b:
               use,psw = line.strip().split()
               if user== use and password==psw:
                   count = 0
                   print("---------欢迎登录-------- ")
    def write_account(user):
        with open("lock_account", 'a') as a:
            a.write(user)
            a.write('
    ')
            exit("输入次数超过3次,你的账户被锁定了")
    while True:
        user=input("请输入您的用户名: ").strip()
        open_lockaccount(user)
        password = input("请输入密码: ").strip()
        count+=1
        flag = False
        open_account(user, password)
        if count == 3 and flag == False:
            write_account(user)
  • 相关阅读:
    Linux的五个查找命令
    Linux中ftp不能上传文件/目录的解决办法
    linux下的chmod,chown和chgrp
    Memcache基础教程
    Web服务器磁盘满故障
    linux定时任务生产java服务无法执行问题案例
    Linux 用户和用户组管理
    10-xargs 简明笔记
    51-du 显示关于目录层次结构或文件磁盘使用情况的信息
    46-df 显示磁盘空间的使用情况
  • 原文地址:https://www.cnblogs.com/sss4/p/6675208.html
Copyright © 2011-2022 走看看