打开文件时 'rb' 和 'wb' 是什么意思?

What do 'rb' and 'wb' mean when opening a file?

Python 3 中二进制模式与默认模式的含义是什么?

当我尝试打开以其他模式保存的 pickle 文件时出现错误,而在 Python 2.7 中我可以毫无问题地在模式之间切换。这个问题很容易解决,但为什么它一开始就很重要?

总的来说:

来自docs

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.

不出所料,您应该对文本数据使用(默认)文本模式,对二进制数据使用二进制模式——包括 pickle 数据,明确 defined 为二进制:

The pickle module implements binary protocols for serializing and de-serializing a Python object structure. “Pickling” is the process whereby a Python object hierarchy is converted into a byte stream, and “unpickling” is the inverse operation, whereby a byte stream (from a binary file or https://docs.python.org/3/glossary.html#term-bytes-like-object) is converted back into an object hierarchy.

Python 3 将文本和二进制数据之间的区别 completely differently 改为 Python 2 – 实际上,这是更改主要版本号的主要原因。因此,有时 "just work" 在 Python 2 中会出现没有充分考虑区别的代码(但随后通常会以意想不到的方式继续下去)。