在什么时候读取或写入文件要使用二进制模式

来源:5-3 YAML文件配置和IO开发

慕村0155948

2021-10-12

如题

写回答

1回答

托尼老师

2021-10-13

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 as bytes 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 as str, the bytes having been first decoded using a platform-dependent encoding or using the specified encoding if given.

从以上官网对源码的解释可以看出,二进制文件IO不会受到编码的影响,以二进制模式读写文件是为了保证文件内容在读写前后的高度一致性。

所以,绝大多数文件的读写都可以使用二进制模式。

0
0

Python自动化测试开发实战,能帮你就业的测试课

开发高扩展、高可用的自动化测试框架,利用好测试自动化技术。

445 学习 · 157 问题

查看课程