Hands-on Lab·Certified Associate Python Programmer
Python File Reading and Writing
PCAP objective 5.5 is perform input/output operations. There are four ways to read a text file and they differ in what they give you and how much memory they use; `readlines` and iteration both hand you lines **with their newlines attached**; and a second `read()` returns an empty string because the file position is already at the end. That last one accounts for most "why is my file empty?" questions.
Files and Streams Guide 24 of 25 Intermediate
- Python3.14.4
- OSUbuntu 26.04 LTS
- pip25.1.1
- TimeAbout 17 min
- Reviewed23 August 2026
Written against the versions above. Unchanged in Python 3, except that iterating a file yields `str` in text mode and `bytes` in binary. `file.next()` from Python 2 is gone; `next(f)` is the Python 3 form and is rarely needed.
| 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
-
Writing a file
-
The four ways to read it back
-
The newline that comes with every line
-
The file position, and the empty second read
-
Counting, which is the standard exercise
-
Handling the file that is not there
-
What the exam does with this objective