zoukankan      html  css  js  c++  java
  • 文件操作续

    今日学习的内容,学习了打开文件的纯净模式,文件内光标移动的操作,以及截断文件,修改文件的操作。

    r+, 读写【可读,可写】,w+,写读【可读,可写】,a+, 写读【可读,可写】
    with open(r'bb.txt',mode='r+',encoding='utf-8') as f:
        print(f.readable())  # 判断是否可读
        print(f.writable())  # 判断是否可写
        print(f.readline())  # 读一行
        f.write('哈哈哈')
    
    w+,写读【可读,可写】
    with open(r'bb.txt',mode='w+',encoding='utf-8') as f:
        print(f.readable())
        print(f.writable())
        print(f.readline())
        f.write('哈哈哈')
    
    with open(r'bb.txt',mode='r+b') as f:
        print(f.readable())
        print(f.writable())
        res = f.read()
        print(res.decode('utf-8'))
        res1 = str(res,encoding='utf-8')
        print(res1)

    文件内光标的移动

    ***在rt模式下 read内的数字 表示的是字符的个数,除此之外,数字表示的都是字节

    with open(r'test','rt',encoding='utf-8') as f:
        print(f.read(5))  # 文本文件中,read读出了五个字符
    
    with open(r'test','rb') as f:
        res = f.read(10)  # 读的是三个字节bytes
        print(res)
        print(res.decode('utf-8'))

    文件光标移动s.seek(offset,whence)

    offset:相对偏移量 光标移动的位数
    whence:
    0:参照文件的开头 t和b都可以使用
    1:参照光标所在的当前位置 只能在b模式下用
    2:参照文件的末尾 只能在b模式下使用

    0模式,参照位置是文件的开头  t和b都可以使用
    with open(r'test','rt',encoding='utf-8') as f:
        print(f.read(1))
        f.seek(6,0)  # seek移动都是字节数
        f.seek(4,0)  # seek移动都是字节数
        print(f.read(1))
        f.seek(0,0)
        print(f.read(1))
        f.seek(0, 0)
        print(f.read(1))
        f.seek(6,0)
        print(f.read())
    
    1模式,参照光标的当前位置  只能在b
    with open(r'test','rb') as f:
        print(f.read(3).decode('utf-8'))
        f.seek(3,1)
        print(f.read(1))
        f.seek(6,0)  # seek移动都是字节数
        f.seek(4,0)  # seek移动都是字节数
    
    2模式参照的位置是文件的末尾   只能在b
    with open(r'test','rb') as f:
        print(f.read())
        f.seek(-4,2)
        print(f.read().decode('utf-8'))

    截断文件

    with open(r'test','a',encoding='utf-8') as f:
        f.truncate(6)  # 接收的字节的长度 整型 保留0~6字节数 后面的全部删除(截断)

    修改文件

    方法一

    1、先将数据由硬盘读到内存(读文件)
    2、在内存中完成修改(字符串的替换)
    3、再覆盖原来的内容(写文件)

    with open(r'cc.txt','r',encoding = 'utf-8') as f:
        data = f.read()  # 将读出的内容赋给data
        print(data)
        print(type(data))  # 类型是字符串
    
    with open(r'cc.txt','w',encoding='utf-8') as f:
        res =data.replace('二哈','橘猫')  # 在data中将“二哈”换成“橘猫”
        print(data)  
        f.write(res)  # 将res的内容写入

    优点:任意时间硬盘上只有一个文件 不会占用过多硬盘空间
    缺点:当文件过大的情况下,可能会造成内存溢出

    方法二

    1、创建一个新文件

    2、循环读取老文件的内容到内存进行修改,将修改好的内容写到新的文件中

    3、将老文件删除,将新文件的名字改成老文件

    import os  # 调用os函数
    with open(r'cc.txt','r',encoding='utf-8') as read_f,  # 打开文件cc.txt
            open(r'cc.wsl','a',encoding ='utf-8') as write_f:  # 创建一个新文件cc.wsl
        for line in read_f:  # 循环读取cc.txt的内容
            new_line = line.replace('柯基','橘猫')  # 将line中的柯基换成橘猫
            write_f.write(new_line)  # 在新文件中写入替换之后的内容
    os.remove('cc.txt')  # 删除原文件
    os.rename('cc.wsl','cc.txt')  # 改名,将新文件的名字改成原文件的名字
  • 相关阅读:
    ubuntu 软件包(package)更换源(source)为阿里云镜像 update&upgrade
    【转载】 ftp 命令行
    阿里云服务器开启端口
    [转载] login shell和non-login shell
    python中global变量释疑
    python 用abc模块构建抽象基类Abstract Base Classes
    【转载】python 特殊函数 dunder function
    tensorflow2.0——波士顿房价数据与多个特征的关系(多元函数的预测)
    tensorflow2.0——波士顿房价数据与房间数关系的预测
    tensorflow2.0——自动求导GradientTape
  • 原文地址:https://www.cnblogs.com/wangnanfei/p/11152637.html
Copyright © 2011-2022 走看看