python - 二进制模式下 io.FileIO 和 open() 的区别

标签 python python-3.x file-io

它们根据校验和产生相同的结果:

with open('openb', 'wb') as f:
    f.write(b'asdf')

with io.FileIO('fileio', 'w') as f:
    f.write(b'asdf')

他们之间有什么区别吗?什么时候应该使用一种方法而不是另一种?

最佳答案

实际上,open()方法会创建一个io.BufferedWriter,它也继承自IOBaseFileIO继承自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/

相关文章:

python - ModuleNotFoundError : No module named 'tf_slim' despite installing

c - 如何有效地从 C 文件中检索数据

windows - 为什么当我尝试打开删除挂起的文件时 Windows 返回 ERROR_ACCESS_DENIED

python - 填充不正确。 AES Python 加密

Python ctypes : Python file object <-> C FILE *

python - 我用这个 python 脚本制作了雪花,但它没有制作雪花

python - "if in"和 "if or if in"之间的差异;寻找解释

python - Gql 在没有祖先本身的情况下获得祖先的所有 child

python - rPython 和 __future__ 导入

haskell - 在 Haskell 中表达一系列脚本化的 Python 字符串替换