Text mode handles encoding/decoding and line endings automatically, while binary mode works with raw bytes. Choice depends on file content and automation requirements.
Text mode ('r', 'w'): For human-readable files (logs, configs, CSVs)
Binary mode ('rb', 'wb'): For executables, images, encrypted files, or when exact byte control is needed
# Text mode - automatic encoding handling
with open('config.txt', 'r', encoding='utf-8') as f:
content = f.read() # Returns string
# Binary mode - raw bytes
with open('backup.tar.gz', 'rb') as f:
content = f.read() # Returns bytes
Rewriting in plainer words…
This answer doesn't lend itself to a diagram - it reads best . No credits were charged.