• day 8


    文件操作

    注意:

    1. r+ 最为常用

    2.encoding 的编码格式一定要与文件编码格式一致

    读取 r  rb

    #在本地创建 txt 格式的文件默认使用 gbk 格式
    f = open('e:/py/file.txt',mode='r',encoding='gbk')
    content = f.read()
    print(content,type(content))
    f.close()
    
    # b 二进制模式 bytes
    f= open('e:/py/file.txt',mode='rb')
    content = f.read()
    print(content,type(content))
    f.close()

    只写  w  wb

    #只写  w
    #我们写入的格式为 utf-8 那么当我们查看时也要使用 utf-8 格式
    #没有该文件 w 会创建文件
    f = open('e:/py/test.txt',mode='w',encoding='utf-8')
    f.write('正在写入...')
    f.close()
    
    
    #存在该文件 会覆盖写入
    #即只写的逻辑为 先清空文件在写入
    f = open('e:/py/test.txt',mode='w',encoding='utf-8')
    f.write('已写入.')
    f.close()
    
    
    #wb 写入
    #默认写入 bytes 类型 需要使用 encode 转换为 str 类型
    #写入与文件默认格式不同的类型 如:utf-8 会自动转换
    f = open('e:/py/test.txt',mode='wb')
    f.write('sfssg个人是的.'.encode('gbk'))
    f.close()

    追加  a  ab

    #追加  a
    #追加与文件默认格式不同的类型 如:utf-8 不会自动转换
    #默认会自动追加在文件光标的位置(即有文字的最后一位上)
    f = open('e:/py/test.txt',mode='a',encoding='gbk')
    f.write('追加...')
    f.close()
    
    
    #ab 追加
    #追加与文件默认格式不同的类型 如:utf-8 不会自动转换
    f = open('e:/py/test.txt',mode='ab')
    f.write('方式...'.encode('utf-8'))
    f.close()

    读写   r+  最常用

    #读写时的文件必须和源文件编码一致
    #读写的位置是在读完后的最有一个字符后面
    #试想在 r+ 模式下 改变读写顺序
    #会在文件开头写入 每写入一个字符便会替换一个字符
    f = open('e:/py/test.txt',mode='r+',encoding='utf-8')
    print(f.read())
    f.write("...")
    f.close()
    
    
    #r+ 也存在 bytes 类型 r+b
    #要与源文件编码一致
    f = open('e:/py/test.txt',mode='r+b')
    print(f.read())
    f.write("你就能看".encode('gbk'))
    f.close()

    写读  w+ 不常用

    #还是先清除 在添加  所以没什么用
    #w+ 也存在 bytes 类型 w+b
    f = open('e:/py/test.txt',mode='w+',encoding='utf-8')
    f.write("...")
    print(f.read())
    f.close()
    
    #w+ 与 seek 调节光标
    #没什么用
    f = open('e:/py/test.txt',mode='w+',encoding='utf-8')
    f.write("...")
    f.seek(0) #光标在 0 的位置
    print(f.read()) #读出结果
    f.close()

    追加 a+

    #与 a 的区别在于 a 只可以执行一个动作
    #a+ 可以执行多步
    #a+ 也存在 bytes 类型 a+b
    f = open('e:/py/test.txt',mode='a+',encoding='gbk')
    f.write('追加...')
    print(f.read())  #这样仍然读不到内容 因为光标在最后
    f.close()
    
    
    # a+ 与 seek
    f = open('e:/py/test.txt',mode='a+',encoding='gbk')
    f.write('追加...')
    f.seek(0)  #光标置于 0 的位置
    print(f.read())
    f.close()

    修改文件

    #把要修改的文件和修改好的部分写入到创建好的新文件中
    #然后删除源文件
    with open('e:/py/test.txt',encoding='gbk')as f,
         open('e:/py/test-bak.txt',"w",encoding='utf-8')as f2:
        for i in f:
            if '发生' in i:
                i=i.replace("发生","产生")
            f2.write(i)  #写文件
    
    import os
    os.remove('e:/py/test.txt')  #删除源文件
    os.rename('e:/py/test-bak.txt','e:/py/test.txt')  #重命名文件

    功能详解

    read 与 seek 

    #按照字符读出  read 读出来的都是字符
    #字符是能看到的最小单位
    #要与源文件编码一致
    f = open('e:/py/test.txt',mode='r+',encoding='gbk')
    print(f.read(3))
    f.close()
    
    
    #read 结合 seek
    #seek 是按照字节来读取的
    f = open('e:/py/test.txt',mode='r+',encoding='gbk')
    f.seek(4)  #gbk 每个汉字 2 个字节  utf-8 每个汉字 3 个字节
    print(f.read())
    f.close()

    tell 获取光标位置

    f = open('e:/py/test.txt',mode='r+',encoding='gbk')
    f.seek(4)  #按照字节定义光标位置
    print(f.tell())
    f.close()

    移动光标读取后三个字

    f = open('d:/py/file.txt',mode='r+',encoding='utf-8')
    f.readline()  #读取一行使光标移动到末尾
    f.seek((f.tell())-9)  #先锁定光标位置 在进行移动
    print(f.read())
    f.close()

    readable、readline、readlines 与 truncate

    f = open('e:/py/test.txt',mode='r+',encoding='gbk')
    #print(f.readable())      #是否可读  返回值:true false
    #print(f.readline())      #每次读一行,即一行一行的读
    #print(f.readlines())     #把每一行当做一个元素,添加到 list 中
    f.truncate(2)             #对源文件截取一段数据用来更改,并覆盖源文件
    f.close() 

     读取第二行内容

    f = open('e:/py/test.txt',mode='r+',encoding='gbk')
    f.readline()     
    f.seek(f.tell())
    print(f.readline())
    f.close()

    读整个文件

    #for 循环写法
    #循环打印出每一行(包括换行符)
    #读文件时 要分段读 因为你不知道文件有多大
    f = open('e:/py/test.txt',mode='r+',encoding='gbk')
    for i in f:
        print(i)
    f.close()
    
    
    #with 写法(自动关闭——close)
    #可以同时打开多个文件
    with open('e:/py/test.txt',mode='r+',encoding='gbk') as file:
        print(file.read())
    
    with open('e:/py/test.txt',mode='r+',encoding='gbk') as file,
    open('e:/py/file.txt',mode='r+',encoding='gbk') as file2:
        print(file.read())
        print(file2.read())
  • 相关阅读:
    redis.conf配置详细解析
    laravel框架的注入
    10 个免费高清图片素材下载网站。#免版权# #设计# #图片处理#
    本地Git连接GitLab(服务器)远程仓库
    基于Docker的Mysql主从复制
    解决git本地代码推服务器每次都要输入用户名和密码的问题
    Laravel上传文件(单文件,多文件)
    php的精度计算问题(bcadd和bcsub)
    POJ 1573 Robot Motion(简单模拟)
    POJ 2996 Help Me with the Game(模拟)
  • 原文地址:https://www.cnblogs.com/ysging/p/9898854.html
Copyright © 2020-2023  润新知