In Python, files on secondary storage are categorized into three distinct formats:
- Text Files (`.txt`): Stored as a sequence of human-readable ASCII or UTF-8 encoded characters. Each line terminates with an End-of-Line (EOL) character (usually `\n` on Linux/macOS, `\r\n` on Windows), which Python automatically translates to standard `\n`.
- Binary Files (`.dat`, `.bin`): Raw binary byte streams directly mirroring memory representations without character translation or EOL delimiters. Used for images, audio, compiled executables, and serialized Python objects.
- CSV Files (`.csv`): Comma-Separated Values text files storing tabular records where fields are delimited by commas (or tabs/semicolons).
The Complete File Access Modes Matrix
| Mode | Type | File Pointer Position | Behavior if File Exists | Behavior if File Missing |
|---|---|---|---|---|
| `'r'` / `'rb'` | Read Only | Beginning (Offset 0) | Opens for reading. | Raises `FileNotFoundError`! |
| `'w'` / `'wb'` | Write Only | Beginning (Offset 0) | TRUNCATES (erases) entire file to 0 bytes! | Creates a brand-new file. |
| `'a'` / `'ab'` | Append Only | End of File | Preserves existing data; appends new data at end. | Creates a brand-new file. |
| `'r+'` / `'rb+'` | Read & Write | Beginning (Offset 0) | Opens for reading and in-place overwriting. | Raises `FileNotFoundError`! |
| `'w+'` / `'wb+'` | Write & Read | Beginning (Offset 0) | Truncates file to 0 bytes, then allows read/write. | Creates a brand-new file. |
| `'a+'` / `'ab+'` | Append & Read | End of File | Preserves existing data; writes strictly append to end. | Creates a brand-new file. |