它们根据校验和产生相同的结果:
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()
方法)相关
关于python - 二进制模式下 io.FileIO 和 open() 的区别,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53432442/