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

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

This guide includes

Use this for the file-operations objective. This matters because every line you read still carries its newline, and a handle remembers where it is - so a second read of the same file gives you nothing.

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