码迷,mamicode.com
首页 > 编程语言 > 详细

Python22 文件读写、StringIO、BytesIO

时间:2019-09-05 22:58:11      阅读:97      评论:0      收藏:0      [点我收藏+]

标签:通过   就是   视频   需要   代码   请求   strip()   write   技术   

文件读写、StringIO、BytesIO

  • IO编程:https://www.liaoxuefeng.com/wiki/1016959663602400/1017606916795776
    - - -
  • 文件读写:,读写文件就是请求操作系统打开一个文件对象(通常称为文件描述符),然后,通过操作系统提供的接口从这个文件对象中读取数据(读文件),或者把数据写入这个文件对象(写文件)。
    • 读文件:使用python内置的函数open() ,传入文件名,和标识符
      • 代码:
        ```
        # 读文件,read.txt 是文件对象,‘r‘ 表示 只读 , 如果文件不存在 就会抛出 IOError
        f = open(‘read.txt‘,‘r‘)
        content = f.read()# 一次性读取文件的全部内容,python把内容读到内存,用一个str对象表示
        print(content)
        f.close() # 最后关闭文件,文件使用完毕后必须关闭,因为文件对象会占用操作系统的资源
        # 读文件三部曲,1.打开文件 2. 读取文件 3. 关闭文件

        IOError:由于文件读写时都有可能产生IOError,一旦出错,后面的f.close()就不会调用

        try:
        f1 = open(‘read.txt‘,‘r‘)
        print(f1.read())
        finally:
        if f1:
        f1.close()

        # with:每次都这么写实在太繁琐,所以,Python引入了with语句来自动帮我们调用close()方法
        with open(‘read.txt‘,‘r‘) as f: # 这和前面的try ... finally是一样的,但是代码更佳简洁,并且不必调用f.close()方法。
        print(f.read())

        # read(size) - 按字节读取内容
        with open(‘read.txt‘,‘r‘) as f:
        # 读取4个字节
        print(f.read(4))

        #readline() - 按行读取内容
        with open(‘read.txt‘,‘r‘) as f:
        print(f.readline())

        # readlines() - 一次读取所有内容并按行返回list
        with open(‘read.txt‘,‘r‘) as f:
        linelst = f.readlines()
        for line in linelst:
        print(line.strip())

        # 二进制文件:读取二进制文件,比如图片、视频等等,用‘rb‘模式打开文件
        with open(‘image.png‘,‘rb‘) as f:
        print(f.read())
        # 字符编码:读取非UTF-8编码的文本文件,需要给open()函数传入encoding参数,例如,读取GBK编码的文件:
        with open(‘gbk.txt‘,‘r‘,encoding=‘gbk‘) as f:
        print(f.read())

        # 处理编码错误 传入 errors
        f = open(‘/Users/michael/gbk.txt‘, ‘r‘, encoding=‘gbk‘, errors=‘ignore‘)
        ```
      • 运行结果:
        技术图片
    • 写文件:
      • 代码:
        ```
        # 写文件:写文件和读文件是一样的,唯一区别是调用open()函数时,传入标识符‘w‘或者‘wb‘表示写文本文件或写二进制文件
        w = open(‘write.txt‘,‘w‘) # ‘w‘ 没有文件时将创建文件,有文件将直接覆盖原文件
        w.write(‘hello‘)
        w.close()

        # ‘a‘ 文件末尾追加内容
        with open(‘write.txt‘,‘a‘) as w: # as w 即 w = with open(‘write.txt‘,‘a‘)
        w.write(‘python3‘)

        # 写特定编码的文件
        w = open(‘write.txt‘,‘w‘,encoding=‘gbk‘)
        w.write(‘gbk‘)
        w.close()
        ```
  • StringIO: 在内存中读写str
    • 代码:
      ```
      # String IO:在内存中读写str
      from io import StringIO
      f = StringIO()
      print(f.write(‘hello‘)) # 打印字节数
      print(f.getvalue()) # getvalue()获取内存中的str

      # 读
      r = StringIO(‘hello\nworld!‘)
      while True:
      s = r.readline()
      if s == ‘‘:
      break
      print(s.strip()) # 替换换行 \n
      ```
  • BytesIO:BytesIO实现了在内存中读写bytes
    • 代码:
      ```
      # BytesIO:操作二进制数据
      from io import BytesIO
      w = BytesIO()
      # 请注意,写入的不是str,而是经过UTF-8编码的bytes。
      print(w.write(‘好运来!‘.encode(‘utf-8‘))) # 打印字节数
      print(w.getvalue()) # 获取二进制值

      # 读
      r = BytesIO(b‘\xe5\xa5\xbd\xe8\xbf\x90\xe6\x9d\xa5\xef\xbc\x81‘)
      print(r.read())
      ```

Python22 文件读写、StringIO、BytesIO

标签:通过   就是   视频   需要   代码   请求   strip()   write   技术   

原文地址:https://www.cnblogs.com/thloveyl/p/11470183.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!