CertGrid CertGrid
Concepts·Certified Associate Python Programmer

Python bytearray and Binary Files

The last part of PCAP objective 5.5. bytes is immutable and bytearray is its mutable sibling - a sequence of integers 0 to 255 that you can assign into. Binary mode (rb, wb) reads and writes those directly, with no decoding, which is the only way to handle a file that is not text. readinto fills a buffer you already own and is the one function here that is genuinely unusual.

Files and Streams Guide 25 of 25 Advanced

bytes and bytearray are Python 3 types. In Python 2, str was bytes and bytearray existed but was rarely used. Everything on this page is Python 3 only.

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 binary end of the file objective. This matters because text mode changes the bytes on the way through - so a binary file opened that way comes back altered, and often will not be read at all.

Before you start

  1. A bytearray is a mutable bytes

  2. The two ways it refuses

  3. Building one

  4. Writing a binary file

  5. readinto, into a buffer you already own

  6. Why text mode is not an option for binary data

  7. What the exam does with this objective

Official sources