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

    1. 操作文件三要素:(打开文件后,一定要关闭)
      a. 路径:E:pythonprojectsaa.txt
      b. 编码:utf-8
      c. 模式:只读 只写 读写

        r:只读 rb:二进制格式读
        w:只写 wb:二进制格式写
        a:追加 ab:二进制格式追加

        r+:能读能写(最常用,最强大)
        w+:写读(先清空再写),文件不存在时创建
        a+:追加写读。如果该文件已存在,追加写。如果该文件不存在,创建新文件用于读写。
        r+b w+b a+b (按二进制格式)

     

    2. 文件对象语法:
      file object = open(file_name [, access_mode][, buffering])

      各个参数的细节如下:
        file_name:file_name变量是一个包含了你要访问的文件名称的字符串值。

        access_mode:
          access_mode决定了打开文件的模式:只读,写入,追加等。
          所有可取值见如下的完全列表。这个参数是非强制的,默认文件访问模式为只读(r)。

        buffering:
          如果buffering的值被设为0,就不会有寄存。
          如果buffering的值取1,访问文件时会寄存行。
          如果将buffering的值设为大于1的整数,表明了这就是的寄存区的缓冲大小。
          如果取负值,寄存区的缓冲大小则为系统默认。

     

    3. 只读 读取utf-8编码数据,转换成Unicode编码的数据 文件在硬盘上时,一定是某种编码,utf-8或gbk等等
      a. mode = "r" 模式 转换
      open(文件路径,encoding= "utf-8",mode=“r”)
      

      b. mode ="rb" 不做转换 读取的时候 rb模式的表现形式 bytes
      open(文件路径,mode=“rb”) 不常用

     

      绝对路径 E:pythonprojectsaa.txt
      相对路径 直接写文件名

    # a-- 读取在硬盘里的utf-8编码数据,读到内存后转换成Unicode编码的数据,在内存里的表现形式为字符串
    obj = open("D:\abc.txt",encoding="utf-8",mode="r")
    content = obj.read()     # 字符串类型
    obj.close()
    print(content)
    # b-- 不设置编码转换的时候,表现形式为bytes类型
    obj = open("D:\abc.txt",mode="rb")
    content = obj.read()     # bytes类型
    obj.close()
    print(content)
    
    obj = open("user.txt",encoding="utf-8",mode="r")
    content = obj.read()     # 字符串类型
    obj.close()
    print(content)
    
    


    4. 只写 不存在时创建,存在时先清空然后覆盖内容

    #a---- open(文件路径,encoding= "utf-8",mode="w")
    obj = open("a.txt",encoding="utf-8",mode="w")
    obj.write("hello word")
    obj.close()
    
    #b----  open(文件路径,mode=“wb”) 不常用
    obj = open("a.txt",mode="wb")
    obj.write("hello word 00000".encode("utf-8"))
    obj.close()
    
    #写完之后不能读
    obj = open("a.txt",mode="w")
    content = obj.read()
    obj.close()
    print(content)
    

      

    5. 只追加 a ******a+只能在最后写******

    obj = open("a.txt",encoding="utf-8",mode="a")
    obj.write("小红")
    obj.close()
    
    obj = open("a.txt",mode="ab")
    obj.write("000".encode("utf-8"))
    obj.close()
    
    # r+  读写  默认在最后写
    obj = open("a.txt",encoding="utf-8",mode="r+")
    data = obj.read()    # 读
    obj.write("gggg")    # 写,单独写的时候,会从头开始写,有内容的话会覆盖
    obj.close()
    print(data)
    
    obj = open("a.txt",encoding="utf-8",mode="r+")
    obj.write('666')    # 写,单独写的时候,会从头开始写,有内容的话会覆盖
    obj.close()
    
    obj = open("a.txt",encoding="utf-8",mode="r+")
    data = obj.read(4)
    obj.write('666')  # 依旧写到最后位置
    obj.close()
    print(data)
    
    obj = open("a.txt",encoding="utf-8",mode="r+")
    data = obj.read(2)
    obj.seek(1)     # seek是字节,汉字三个字节一位,标明了要写入的位置
    obj.write("0")    # 单独写的时候,会从头开始写,有内容的话会覆盖
    obj.close()       # 指定写的时候,有内容的话会覆盖,(只是看不到,)
    print(data)
    

      

    常用的文件对象方法:

    # read(读到第几个字符(r)字节(rb))   默认读取所有文件
    obj = open("a.txt",encoding="utf-8",mode="r+")
    data = obj.read(2)
    print(data)
    
    obj = open("a.txt",mode="r+b")
    data = obj.read(2)
    print(data)
    
    # write("字节(wb)或字符串(w)")#不存在时创建,存在时先清空然后覆盖内容
    obj = open("a.txt",encoding="utf-8",mode="w+")
    obj.write("2")
    obj.close()
    
    # seek(n,location)  指定位置写并覆盖 指定指针位置  不管是r还是rb  n永远都是字节 ;location指针位置,可以不写,0,1,2是三个模式,0是开头,1是默认,2是最后
    obj = open("a.txt",encoding="utf-8",mode="r+")
    obj.seek(1)     # seek是字节,汉字三个字节一位,标明了要写入的位置
    data = obj.read(2)
    obj.close()
    print(data)
    
    # **tell()  读取当前指针在哪
    obj = open("a.txt",encoding="utf-8",mode="r+")
    
    # obj.tell()
    # obj.close()
    num = int(input("请输入要读取的字符:"))    # 因为编码不同,当文件里是汉字时,写入数字和字母会出现错误,因为字母,数字和汉字所占的位数不同导致的
    data = obj.read(num)
    obj.seek(obj.tell())
    obj.write("34")
    
    flush()     # 将内存中的内容强制刷到硬盘
    obj = open("aaa",encoding="utf-8",mode="w")
    while True:
        data = input(">>>")
        obj.write(data)     # 因为无法执行close,所以写的只能写到内存中
        obj.flush()     # 强刷会把内容强制刷到硬盘里,就能读到东西了
    obj.close()
    
    obj = open("aaa",encoding="utf-8",mode="r"+)
    val = obj.read()
    print(val)
    
    readline()      # 只读一行
    obj = open("a.txt",encoding="utf-8",mode="r+")
    data = obj.readline()
    print(data)     # 读一次之后,指针在第2个位置,再次去读就会从下一个位置开始读
    data = obj.readline()
    print(data)
    
    readlines()      # 按行读到所有东西
    xreadlines()   # py3里没有这个功能了
    
    #重点:
    # for line in obj : 把每一行都打印出来        等价于py2里的 xreadlines()
    obj = open("a.txt",encoding="utf-8",mode="r+")
    for line in obj:
        print(line)
    obj.close()
    
    # 手动关毕
    # 打开文件后,一定要关闭
    
    obj = open(文件路径,mode=rb)
    xxxx
    obj.close()
    
    #自动关闭
    with open(文件路径,mode= "rb") as obj:
         xxx  # 代码执行完毕后,自动关闭文件
    
    truncate()       # 截取文件内容,根据指针位置,只保留之前的数据
    obj = open("a.txt",encoding="utf-8",mode="r+")
    obj.seek(3)
    obj.truncate()
    obj.close()
    
    writeable()     # 是否可写
    # 以r 模式打开的时候不可写
    
    readable()     #是否可读
    # 以w 模式打开的时候不可读
    
    fileno()   # 文件描述符,就是一个数字
    

      

  • 相关阅读:
    poj2115
    poj2136
    软件的架构与设计模式之模式的种类
    SOA: My Understanding (转载未来技术方向)
    深度管理Remote Objects的生存期 (转载)
    SmartClient(智能客户端) 转载
    Windows Communication Foundation入门(Part One)
    使用.NET Remoting开发分布式应用——基于租约的生存期(转载)
    软件设计模式C#
    MarshalByRefObjects远程对象及其调用方法
  • 原文地址:https://www.cnblogs.com/chitalu/p/9674875.html
Copyright © 2011-2022 走看看