首页 > 代码库 > python文件的增删改查操作

python文件的增删改查操作

技术分享
  1 def choice():
  2     print(‘‘‘
  3     1   查找
  4     2   删除
  5     3   添加
  6     4   修改
  7     ‘‘‘)
  8 def forlist():
  9     with open("test2", encoding="utf8", mode="r+") as w:
 10         for line in w:
 11             print(line.strip())
 12 
 13 
 14 def check():
 15     usr=input("输入你要查找的域名")
 16     l=[]
 17     flag=False
 18     with open("test",encoding="utf8") as f:
 19         for line in f:
 20             if line.startswith("backend") and usr in line:
 21                 flag = True
 22                 continue
 23             if line.startswith("backend") and flag:
 24                 break
 25             if flag:
 26                 l.append(line.strip())
 27         for i in l:
 28             print (i)
 29 
 30 def remove():
 31     usr=input("输入你要移除的域名")
 32     flag = True
 33     # www.oldboy1.org
 34     with open("test", encoding="utf8") as f:
 35         with open("test2", encoding="utf8", mode="r+") as w:
 36             for line in f:
 37                 if line.startswith("backend") and usr in line:
 38                     flag = False
 39                     continue
 40                 if line.startswith("backend"):
 41                     flag = True
 42                 if flag:
 43                     w.write(line)
 44             forlist()
 45 
 46 def add():
 47     usr=input("输入你要添加的内容")
 48     print({bakend: www.oldboy.org,
 49          record: {server: 100.1.7.9, weight: 20, maxconn: 30}})
 50     arg=eval(usr)
 51     add = """%s %s
 52             %s %s %s %s %s %s %s
 53     """ % (bakend, arg[bakend], record, server, arg[record][server],
 54            weight, arg[record][weight], maxconn, arg[record][weight])
 55     flag = True
 56     with open("test1", encoding="utf8", mode="r+") as f, open("test2", encoding="utf8", mode="w+") as w:
 57         for line in f:
 58             if line.startswith("backend") and "www.oldboy1.org" in line:
 59                 w.write(add)
 60                 print()
 61             if line.startswith("backend"):
 62                 flag = True
 63             if flag:
 64                 w.write(line)
 65         forlist()
 66 
 67 def change():
 68     usr=input("输入你要修改的内容")
 69     arg=eval(usr)
 70     print({bakend: www.oldboy.org,record: {server: 100.1.7.9, weight: 20, maxconn: 30}})
 71     add = """%s %s
 72             %s %s %s %s %s %s %s
 73     """ % (bakend, arg[bakend], record, server, arg[record][server],
 74            weight, arg[record][weight], maxconn, arg[record][weight])
 75     # usr=input(">>>")
 76     flag = True
 77     with open("test1", encoding="utf8", mode="r+") as f, open("test2", encoding="utf8", mode="w") as w:
 78         for line in f:
 79             if line.startswith("backend") and www.oldboy1.org in line:
 80                 flag = False
 81                 continue
 82             if line.startswith("backend"):
 83                 w.write(add)
 84                 flag = True
 85             if flag:
 86                 w.write(line)
 87         forlist()
 88 
 89 def main():
 90     while True:
 91         choice()
 92         usr=input("输入你要做的操作")
 93         if usr=="1":
 94             check()
 95         elif usr=="2":
 96             remove()
 97         elif usr=="3":
 98             add()
 99         elif usr=="4":
100             change()
101         elif usr=="q":
102             break
103 main()
文件操作

在文件的操作中,必须注意文件的修改和增加都不会在源文件中进行插入写入操作,而是需要定义新的文件来接受源文件值及修改内容,然后对文件进行更名后替换源文件

python的文件操作使用函数open对文件进行打开操作

例如open,格式如下:

        f=open("文件名",encoding="解码格式",mode="文件的打开模式")   

当文件打开时,需要注意文件的编码方式,而文件默认的编码方式会以操作系统的编码方式一致,如果文件编码方式与解释器不同,需要对文件进行编码后才能顺利打开

在python中,文件的打开模式有:

    r  读模式    文件会以只读方式打开,无法对文件进行写操作

    w  写模式  文件会以写入模式打开,由于文件存储的特性,写模式会以覆盖源内容的方式对文件进行写操作

    a  追加模式   文件内添加新的内容不会覆盖源文件内容,会以内容追加方式添加到新的文件中

    r+  读写模式,文件在读的基础上对文件进行文件写入

    w+  读写模式,文件添加会覆盖源内容

    a+  追加读写模式

在python文件操作中,文件的读模式在读取文件时,会以字符位读取文件

在python2.0版本中,文件的读取模式在读取文件是会以字节位读取

未完待续……

 

python文件的增删改查操作