Binary and text modes
At the filesytem level, of course, files contain only a series of bytes. Python, however, distinguishes between files opened in binary and text modes, even when the underlying operating system doesn't. When you open a file in binary mode, you are instructing Python to use the data in the file without any decoding; binary mode file reflects the raw data in the file.
A file opened in text mode, on the other hand, treats its contents as if it contains text strings of the str type. When you get data from a text mode file, Python first decodes the raw bytes using either a platform-dependent encoding or, if provided, the encoding argument to open().
By default, text mode files also engage support for Python's universal newlines ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access