我试图理解 io.BytesIO 的 write() 和 read() 方法。我的理解是,我可以像使用文件对象一样使用 io.BytesIO。
import io
in_memory = io.BytesIO(b'hello')
print( in_memory.read() )
上面的代码将如预期地返回b'hello',但下面的代码将返回空字符串b''。
import io
in_memory = io.BytesIO(b'hello')
in_memory.write(b' world')
print( in_memory.read() )
我的问题如下:
- io.BytesIO.write(b' world')
究竟是在做什么?
- io.BytesIO.read() 和 io.BytesIO.getvalue() 有什么区别?
我猜想答案与 io.BytesIO 是一个流对象有关,但总体情况对我来说不是很清晰。