zoukankan      html  css  js  c++  java
  • 文件读写

    持久化保存,需要保存在硬盘中

    文件的读取:

    #打开文件

    #读/写文件

    #关闭文件

    读文件

    用r

    调用open函数(‘打开文件绝对地址,‘读,写模式’,‘encoding:utf-8’)

    eg:

    read#打印全部

    f = open(r'C:UsersadminDesktop新建文本文档.txt','r',encoding='utf-8')

    result= f.read()#读文件,获取文件里面的全部内容

    print(result)

    f.close()#关闭文件

    readline #只读取一次只读取第一行

    f = open(r'C:UsersadminDesktop新建文本文档.txt','r',encoding='utf-8')
    
    result= f.readline()#读文件
    print(result)
    print(f.readline())#打印下一行
    f.close()#关闭文件

    readlines#打印去全部内容,返回的是一个list,每行的内容放是list一个元素

    f = open(r'C:UsersadminDesktop新建文本文档.txt','r',encoding='utf-8')
    
    result= f.readlines()#读文件
    print(result)
    
    f.close()#关闭文件

    文件指针:记录读在哪一行

    大文件处理:

    f = open(r'C:UsersadminDesktop新建文本文档.txt','r',encoding='utf-8')
    for line in f :
        print(line)
    f.close()

    写文件:

    写文件用w,会清空之前的内容清空

    f = open(r'C:UsersadminDesktop新建文本文档.txt','w',encoding='utf-8')
    f = open(r'C:UsersadminDesktop新建文本文档.txt','w',encoding='utf-8')
    f.write('你好啊
    ,你真好
    ')
    f.close()
    #传字符串直接用writelines()
    #传数组用write
    f = open(r'C:UsersadminDesktop新建文本文档.txt','w',encoding='utf-8')
    l=['a
    ','b
    ']
    for i in l:
         f.write(i+'/n')#或者用这种
    f.readlines(l)

     a模式:

    追加模式

    f = open(r'C:UsersadminDesktop新建文本文档.txt','a',encoding='utf-8')
    f.write('我啊')
    f.close()

     r+(可以写如果这个文件不存在,会报错,有文件会写在最前面),读的没有问题。

    会覆盖第一行

    f = open(r'C:UsersadminDesktop新建文本文档.txt','r+',encoding='utf-8')
    f.write('~~~')
    print(f.read())
    f.close()

     w+:之前的文件会被清空

    f = open(r'C:UsersadminDesktop新建文本文档.txt','r+',encoding='utf-8')
    f.write('~~~')
    print(f.read())
    f.close()

    a+:
    可以读可以写
    f = open(r'C:UsersadminDesktop新建文本文档.txt','a+',encoding='utf-8')
    f.seek(0)
    f.truncate #清空文件
    print(f.read())
    f.write('xxx') f.close()

     修改文件

     1.简单粗暴(小文件用这种方法 )

    1.读取文件所有内容

    2.repalace

    3.清空文件

    4.把新的内容写进去

    replace
    #修改
    with open  (r'C:UsersadminDesktop新建文本文档.txt','r+',encoding='utf-8')as f:
        content = f.read()
        new_content = content.replace('你说','说我')#修改
        f.seek(0)
        f.truncate()
        f.write(new_content)
    f.flush()如果没有看到数据变,用flush 立即刷新缓冲区的内容,写到磁盘上

     1.两个文件操作

    r模式打开a文件,w模式打开b文件

    支行读取a文件的内容,读完之后替换内容

    把a文件删掉,把b文件的名字改成a

    import os #删除文件

    with open (r'C:UsersadminDesktop新建文本文档.txt','r+',encoding='utf-8')as f1,open(r'C:UsersadminDesktopha.txt','w',encoding='utf-8')as f2: for line in f1: new= line.replace('说我','nihao ') f2.write(new)
    os.remove(
    r'C:UsersadminDesktop新建文本文档.txt')
    os.remove(
    r'C:UsersadminDesktopha.txt',(r'C:UsersadminDesktopha.txt')

     

    # 1.
    # 监控,一致在云运行,死循环,每分钟读一次
    #
    # 2.
    # 第一次运行,读取文件所有的内容,从文件内容取到ip, 每个IP出现的次数
    #
    # 3.
    # 判断每个ip地址出现的次数,如果大于50次,那么发送报警
    #
    # 4.
    # 等待60S,继续重复
    import time
    point = 0#最前面的位置
    while True:
        f = open(r'C:UsersadminDesktopaccess.log',encoding='utf-8')
        f.seek(point)#移动文件指针到那个位置
        ip_infos = {}
        for line in f :
            ip = line.split()[0]
            if ip not in ip_infos:
                ip_infos[ip] =1
            else:
                ip_infos[ip]+=1
        point = f.tell()#当前文件指针的位置
        for ip in ip_infos:
            count = ip_infos.get(ip)
            if count >=50:
                print('ip地址{}有问题,请注意.format(),ip')
        f.close()
        time.sleep(60)
    
    
    
    
    
    
    
    
    
    
    

    其他问题:

    如果出现这样报错,将本文 另存为将格式改utf-8

    "D:Program Files (x86)PythonPython37-32python.exe" D:/Users/admin/PycharmProjects/untitled4/w.py
    Traceback (most recent call last):
      File "D:/Users/admin/PycharmProjects/untitled4/w.py", line 46, in <module>
        e= f.readline()#读文件
      File "D:Program Files (x86)PythonPython37-32libcodecs.py", line 322, in decode
        (result, consumed) = self._buffer_decode(data, self.errors, final)
    UnicodeDecodeError: 'utf-8' codec can't decode byte 0xce in position 0: invalid continuation byte
  • 相关阅读:
    GIL
    CRM2Stark组件
    Django图书管理系统(单表操作)
    Python(ATM机low版)
    Python(9-18天总结)
    Python(1-8天总结)
    Python习题(分页显示)
    Python文本操作2
    Python递归二分法
    Python文本操作
  • 原文地址:https://www.cnblogs.com/xxxxyanyan/p/12852531.html
Copyright © 2011-2022 走看看