二进制模式下 io.FileIO 和 open() 的区别
Difference between io.FileIO and open() in binary mode
它们基于校验和产生相同的结果:
with open('openb', 'wb') as f:
f.write(b'asdf')
with io.FileIO('fileio', 'w') as f:
f.write(b'asdf')
它们之间有什么区别吗?什么时候应该使用一种方法而不是另一种?
实际上 open()
方法将创建一个 io.BufferedWriter
,它继承自 IOBase
,FileIO
也继承自 IOBase
。尽管不完全相同 类,但它们支持的功能基本相同。如果您知道不需要缓冲,我想您可以使用 FileIO。 python io docs page 上有很多信息
其中最重要的一句话是:
Raw binary I/O typically provides low-level access to an underlying OS device or API, and does not try to encapsulate it in high-level primitives (this is left to Buffered I/O and Text I/O, described later in this page).
原始二进制文件 I/O 与 io.FileIO 相关,缓冲 I/O 与 io.BufferedWriter(open()
方法)
它们基于校验和产生相同的结果:
with open('openb', 'wb') as f:
f.write(b'asdf')
with io.FileIO('fileio', 'w') as f:
f.write(b'asdf')
它们之间有什么区别吗?什么时候应该使用一种方法而不是另一种?
实际上 open()
方法将创建一个 io.BufferedWriter
,它继承自 IOBase
,FileIO
也继承自 IOBase
。尽管不完全相同 类,但它们支持的功能基本相同。如果您知道不需要缓冲,我想您可以使用 FileIO。 python io docs page 上有很多信息
其中最重要的一句话是:
Raw binary I/O typically provides low-level access to an underlying OS device or API, and does not try to encapsulate it in high-level primitives (this is left to Buffered I/O and Text I/O, described later in this page).
原始二进制文件 I/O 与 io.FileIO 相关,缓冲 I/O 与 io.BufferedWriter(open()
方法)