text mode
The default way of opening a file, which decodes bytes into str using an encoding.
In more detail
In text mode read() returns strings, write() takes strings, and line endings are converted to a single newline. The encoding, usually UTF-8, turns bytes into characters, and bytes that are invalid in it raise UnicodeDecodeError. Add t to the mode or leave it out; text is the default.
Example
with open('notes.txt', encoding='utf-8') as f:
text = f.read() # strRelated terms
- binary mode — Opening a file with b in its mode, so reads return bytes exactly as stored.
- open() — The built-in function that opens a file by name and returns a file object, for reading by default or for writing or appending.
- UTF-8 — The most widely used character encoding, which can represent every Unicode character and matches ASCII for plain English letters.
Taught in
- Streams, Handles And Open Modes — PCAP Certified Associate in Python Programming (PCAP-31-03) Exam Prep