在什么时候读取或写入文件要使用二进制模式
来源:5-3 YAML文件配置和IO开发

慕村0155948
2021-10-12
如题
写回答
1回答
-
As mentioned in the Overview, Python distinguishes between binary and text I/O. Files opened in binary mode (including
'b'
in the mode argument) return contents asbytes
objects without any decoding. In text mode (the default, or when't'
is included in the mode argument), the contents of the file are returned asstr
, the bytes having been first decoded using a platform-dependent encoding or using the specified encoding if given.从以上官网对源码的解释可以看出,二进制文件IO不会受到编码的影响,以二进制模式读写文件是为了保证文件内容在读写前后的高度一致性。
所以,绝大多数文件的读写都可以使用二进制模式。
00
相似问题