ajeje
ajeje

Reputation: 610

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

What are the implications of the binary vs default mode in Python 3?

When I try to open a pickle file that was saved in the other mode I get an error, while in Python 2.7 I could switch between modes without any issue. The problem can be easily fixed, but why does it matter in the first place?

In general:

Upvotes: 0

Views: 9938

Answers (1)

schesis
schesis

Reputation: 59168

From the 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.

Unsurprisingly, you should use the (default) text mode for text data, and binary mode for binary data – including pickle data, which is explicitly defined as binary:

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 approaches the distinction between text and binary data completely differently to Python 2 – indeed, this was the primary reason for the change of major version number. As a result, it's sometimes the case that code which does not adequately consider the distinction appears to "just work" in Python 2 (but will then often bite you in unexpected ways further down the line).

Upvotes: 4

Related Questions