Python File Streams, Modes and Handles
PCAP objective 5.4 is understand basic input/output terminology, and it is the vocabulary objective for files: stream, handle, mode, text against binary. `open()` returns an object with a `name`, a `mode` and an `encoding`; the mode letter decides what you may do and whether the file is truncated; and five distinct things can go wrong, each with its own exception.
Files and Streams Guide 23 of 25 Intermediate
- Python3.14.4
- OSUbuntu 26.04 LTS
- pip25.1.1
- TimeAbout 16 min
- Reviewed23 August 2026
Written against the versions above. `open()` returns a `TextIOWrapper` in text mode in Python 3 and the `io` module is the machinery behind it. Python 2's `file` type no longer exists. The default encoding for `open()` is locale-dependent - UTF-8 on this machine - which is why passing `encoding=` explicitly is good practice.
| Server Name | IP Address | OS | Roles | CPU | RAM | HDD |
|---|---|---|---|---|---|---|
| RUNNER01 | 192.168.0.27 | Ubuntu 26.04 LTS | Python 3.14.4 - the only machine this path needs | 2 Core | 4 GB | 50 GB |
Before you start
-
What open returns
-
The mode letters
-
The five things that go wrong
-
with closes the file whatever happens
-
The three streams that are always open
-
What the exam does with this objective