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. This causes translation between a single portable newline character in our program strings (' ') and a platform dependent newline representation in the raw bytes stored in the file system (for example carriage-return-newline (' ') on Windows).

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset