CertGrid CertGrid
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

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.

One machine, and any shell with Python 3 will do - these exams test the language, not a distribution.
Server NameIP AddressOSRolesCPURAMHDD
RUNNER01192.168.0.27Ubuntu 26.04 LTSPython 3.14.4 - the only machine this path needs2 Core4 GB50 GB

Before you start

  1. Writing a file

  2. The four ways to read it back

  3. The newline that comes with every line

  4. The file position, and the empty second read

  5. Counting, which is the standard exercise

  6. Handling the file that is not there

  7. What the exam does with this objective

Official sources