Phase 11 of 25 · Topic 11.1

File Handling: Text vs Binary Modes & Encodings (UTF-8)

1Concept

Text mode (`'r'`, `'w'`) reads and writes string objects (`str`), automatically translating bytes using character encoding (defaulting to UTF-8 in modern Python). Binary mode (`'rb'`, `'wb'`) operates on raw byte objects (`bytes`), essential for image, audio, and compressed stream processing.

2Architecture Diagram

Text Stream:   [ Encoded Bytes on Disk ] ---> Decoded via UTF-8 ---> [ Python str ]
Binary Stream: [ Raw Bytes on Disk ]    ---> Passed as-is        ---> [ Python bytes ]

3Code Example

Python 3.12
import io

# Binary stream processing in memory
binary_buffer = io.BytesIO(b"\x89PNG\r\n\x1a\n\x00\x00\x00\rIHDR")
header = binary_buffer.read(8)
print(f"Read binary signature: {header}")
print(f"Is PNG Signature: {header.startswith(b'\x89PNG')}")

# Text stream with explicit UTF-8 encoding
text_buffer = io.StringIO("Enterprise Python Engineering 2026")
print(f"Read text stream: {text_buffer.readline()}")

4Expected Output

Read binary signature: b'\x89PNG\r\n\x1a\n'
Is PNG Signature: True
Read text stream: Enterprise Python Engineering 2026

5Key Takeaways

  • ALWAYS specify `encoding='utf-8'` explicitly when opening text files to avoid platform-dependent locale bugs (e.g. Windows cp1252).
  • Binary mode does NOT perform newline translation (`\r\n` to `\n`).
  • Use `io.StringIO` and `io.BytesIO` for in-memory stream testing without disk I/O.