bmwm3 发表于 2018-8-12 08:57:44

python之路----文件操作

import os  

  
def operation():            #提供给用户的选择
  

  
    oper_dict = {'1':'find','2':'change','3':'add','4':'remove'}
  

  
    for key in oper_dict:
  

  
      print(key,oper_dict)
  

  
    select = input("请选择操作:").strip()
  

  
    if select == '1' or select == 'find':
  

  
      find()
  

  
    if select == '2' or select == 'change':
  

  
      change()
  

  
    if select == '3' or select == 'add':
  

  
      add()
  

  
    if select == '4' or select == 'remove':
  

  
      remove()
  

  
def find():
  

  
    out_flag = True          #用来跳出外层循环的标志位
  

  
    while out_flag:
  

  
      flag = False
  

  
      l = []
  

  
      with open('haproxy.conf','r',encoding='utf-8') as f_read:   #以读的方式打开文件
  

  
            choice = input("请输入要查找的URL:").strip()
  

  
            for line in f_read:                        #遍历文件的每行并进行匹配
  

  
                if line.startswith("backend") and choice in line:
  

  
                  flag = True
  

  
                  continue
  

  
                if line.startswith("backend") and flag:
  

  
                  break
  

  
                if flag:
  

  
                  l.append(line.strip())            #将匹配到的URL之中的记录存放到l中
  

  
      for i in l:                                    #输入结果
  

  
            print(i)
  

  
      w = input("是否继续查询?y/n").strip()
  

  
      if w == 'y' or w == 'Y' or w == 'yes':
  

  
            continue
  

  
      if w == 'n' or w == 'N' or w == 'no':
  

  
            out_flag = False
  

  
    # os.rename("haproxy.conf","haproxy.conf.bak")#将原文件更名,并将修改后的文件重命名
  

  
    # os.rename("test","haproxy.conf")            #为了便于演示,这两行均没有运行
  

  
def change():
  

  
    flag = False
  

  
    l = []
  

  
    with open('haproxy.conf', 'r', encoding='utf-8') as f_read,open('test','w',encoding='utf-8') as f_write:
  

  
      choice = input("请输入要更改的记录所属的URL:").strip()
  

  
      for line in f_read:                           #与查找功能一样
  

  
            if line.startswith("backend") and choice in line:
  

  
                flag = True
  

  
                continue
  

  
            if line.startswith("backend") and flag:
  

  
                break
  

  
            if flag:
  

  
                l.append(line.strip())
  

  
      count = 0
  

  
      for i in l:
  

  
            print(count,i)
  

  
            count += 1
  

  
      record = int(input("请输入要修改的记录编号:").strip())
  

  
      new_record = input("请输入新记录:").strip()
  
                     #将输入的新记录覆盖到之前查出的列表中
  

  
      l = new_record
  

  
    sign = False
  

  
    times = 0
  

  
    with open('haproxy.conf', 'r', encoding='utf-8') as f_read, open('test', 'w', encoding='utf-8') as f_write:
  

  
      for line in f_read:
  

  
            if line.startswith("backend") and choice in line:
  

  
                sign = True
  

  
                f_write.write(line)
  

  
                continue
  

  
            if line.startswith("backend") and sign:
  

  
                sign = False
  

  
                f_write.write(line)
  

  
                continue
  

  
            if sign:
  

  
                if times < len(l):
  

  
                  f_write.write('\t\t'+l+'\n')
  
                                     #将修改后的列表以行的方式写到新文件中
  

  
                  times += 1
  

  
                continue
  

  
            else:
  

  
                f_write.write(line)
  

  
    # os.rename("haproxy.conf","haproxy.conf.bak")
  

  
    # os.rename("test","haproxy.conf")
  

  
def add():
  

  
    flag = False
  

  
    l = []
  

  
    with open('haproxy.conf', 'r', encoding='utf-8') as f_read, open('test', 'w', encoding='utf-8') as f_write:
  

  
      choice = input("请输入要添加的记录所属的URL:").strip()
  

  
      record = input("请输入要添加的记录:").strip()
  

  
      for line in f_read:
  

  
            if line.startswith("backend") and choice in line:
  

  
                flag = True
  

  
                f_write.write(line)
  

  
                f_write.write('\t\t'+record+'\n')
  
                                     #查询到需要添加的URL后,在下一行添加需要增加的行即可
  

  
                continue
  

  
            if line.startswith("backend") and flag:
  

  
                flag = False
  

  
                f_write.write(line)
  

  
                continue
  

  
            if flag:
  

  
                f_write.write(line)
  

  
                continue
  

  
            else:
  

  
                f_write.write(line)
  

  
    # os.rename("haproxy.conf","haproxy.conf.bak")
  

  
    # os.rename("test","haproxy.conf")
  

  
def remove():
  

  
    flag = False
  

  
    l = []
  

  
    with open('haproxy.conf', 'r', encoding='utf-8') as f_read, open('test', 'w', encoding='utf-8') as f_write:
  

  
      choice = input("请输入要删除的记录所属的URL:").strip()
  

  
      for line in f_read:
  

  
            if line.startswith("backend") and choice in line:
  

  
                flag = True
  

  
                continue
  

  
            if line.startswith("backend") and flag:
  

  
                break
  

  
            if flag:
  

  
                l.append(line.strip())
  

  
      count = 0
  

  
      for i in l:
  

  
            print(count, i)
  

  
            count += 1
  

  
      record = int(input("请输入要删除的记录编号:").strip())
  

  
      l.pop(record)
  

  
    sign = False
  

  
    times = 0
  

  
    with open('haproxy.conf', 'r', encoding='utf-8') as f_read, open('test', 'w', encoding='utf-8') as f_write:
  

  
      for line in f_read:
  

  
            if line.startswith("backend") and choice in line:
  

  
                sign = True
  

  
                f_write.write(line)               #没有进行修改的行正常写入
  

  
                continue
  

  
            if line.startswith("backend") and sign:
  

  
                sign = False
  

  
                f_write.write(line)               #没有进行修改的行正常写入
  

  
                continue
  

  
            if sign:
  

  
                if times < len(l):
  

  
                  f_write.write('\t\t' + l + '\n')   #将删除后的列表写到新文件里
  

  
                  times += 1
  

  
                continue
  

  
            else:
  

  
                f_write.write(line)
  

  
    # os.rename("haproxy.conf","haproxy.conf.bak")
  

  
    # os.rename("test","haproxy.conf")
  

  
operation()
页: [1]
查看完整版本: python之路----文件操作