CertGrid CertGrid
Concepts·Certified Associate Python Programmer

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

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.

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 input/output terminology objective. This matters because five different things go wrong when you open a file, each with its own exception - and telling them apart is what the questions ask.

Before you start

  1. What open returns

  2. The mode letters

  3. The five things that go wrong

  4. with closes the file whatever happens

  5. The three streams that are always open

  6. What the exam does with this objective

Official sources