← QCE Digital Solutions Unit 4

Reading and writing binary files

Opens a file in binary mode so read() returns raw bytes, not text. Use 'wb' to write bytes. Binary data is type bytes, not str, and read(n) reads n bytes.

Understanding Reading and writing binary files in QCE Digital Solutions Unit 4

What it does: read(n) on a binary file returns up to n bytes as a bytes object, so read(4) gets 4 bytes. The b before the quotes makes a bytes value, which is what a file opened with 'wb' accepts. 'wb' means write binary. 'w' writes text (str), not bytes. Images are binary data. Reading them in text mode tries to decode them as characters and can fail or corrupt the data. Speed is not the reason.

Common mistakes: read is a method, so the number of bytes goes in round brackets: f.read(4). Square brackets are for indexing, not for calling a method. A file opened with 'wb' only accepts bytes. str(data) turns it into text, which raises a TypeError. It should be f.write(data).

Class: _____________________  ·  Name: _____________________  ·  Date: _________

Free printable worksheet with full answer key — or add this module to a class and your students practise it interactively.

This module's key terms are also in a printable crossword (Modules 334–401) and word search (Modules 127–401).

Take this worksheet further — free with Cloodit

Each concept is reinforced from every angle — learned, then applied, then tested.

Learning
Duo
Which line opens a file so read() returns raw bytes?
open('data.bin', 'r')open('data.bin', 'rb')
Matches
f = open('data.bin', 'wb') f.write('Hello')
↓
A str is written to a file in 'wb' mode. Binary mode needs bytes, so use b'Hello' or 'Hello'.encode().
Applying
Meanings
Term
open('data.bin', 'rb')
Meaning
Opens a file in binary mode so read() returns raw bytes, not text. Use 'wb' to write bytes. Binary data is type bytes, not str, and read(n) reads n bytes.
Practice
f = open('data.bin', 'wb')
Testing
Gaps
with open('data.bin', '') as f:
Check Answer
Spots
1f = open('data.bin', 'rb')
2data = f.read[4]

Cloodit turns curriculum-aligned coding content into journeys students work through one biome at a time.

The 'Arctic Hare' badge — Speed Indexer, one of Cloodit's 106 collectible badges
Students earn collectable badges

Over 100 of them, across seven natural biomes.

Study Buddy
Students build a real study resource

Turns their work into revision material ready for Flash Cards and Exam study.

Diamond Dash — one of Cloodit's live, competitive Sprint games
Students lock in with Sprints

Live, competitive practice for the start of the lesson or a teacher-led lesson finisher.

A Cloodit certificate of achievement, ready to personalise and print
Custom printable certificates

Honour your top performers, or encourage a student who's trying hard — ready to print in seconds.

Quick Questions

1. Which line opens a file so read() returns raw bytes?
A. open('data.bin', 'rb') ✓
B. open('data.bin', 'r')
'rb' means read binary, so read() gives you bytes. 'r' reads text and gives you a str.
2. Which mode do you use to write raw bytes to a file?
A. 'wb' ✓
B. 'w'
'wb' means write binary. 'w' writes text (str), not bytes.
3. What type does f.read() return when f was opened with 'rb'?
A. bytes ✓
B. str
In binary mode, read() returns a bytes object. A str only comes back in text mode.
4. Which write call works on a file opened with 'wb'?
A. f.write('Hi')
B. f.write(b'Hi') ✓
A file opened with 'wb' needs bytes. The b prefix makes b'Hi' a bytes value. Writing a plain str raises a TypeError.
5. A file is opened with open('pic.png', 'rb'). What does f.read(4) return?
A. The first 4 lines of the file
B. The first 4 bytes of the file ✓
In read(n), n is a number of bytes, not lines. Binary files have no concept of text lines.

Match the Pairs

f = open('data.bin', 'rb') → Opens the file in binary read mode so reading gives raw bytes instead of text
f = open('copy.bin', 'wb') → Opens the file in binary write mode so you can write raw bytes to it
Reading exactly 4 bytes from a file opened in binary mode → chunk = f.read(4)
Binary data is type bytes, not str, so a b prefix is used for a bytes literal → f.write(b'AB')
f = open('data.bin', 'wb') f.write('Hello') → A str is written to a file in 'wb' mode. Binary mode needs bytes, so use b'Hello' or 'Hello'.encode().
f = open('data.bin', 'r') data = f.read(4) → The file is opened in text mode 'r', so read(4) gives 4 characters as str, not 4 raw bytes. Use 'rb'.

Key Term

open('data.bin', 'rb')
Opens a file in binary mode so read() returns raw bytes, not text. Use 'wb' to write bytes. Binary data is type bytes, not str, and read(n) reads n bytes.

Fill in the Blanks

1. with open('data.bin', 'rb') as f: data = f.read() print(type(data))
Hint: Type the mode letters inside the quotes that open a file for reading raw bytes.
2. with open('copy.bin', 'wb') as f: f.write(bytes([65, 66, 67]))
Hint: Type the mode letters inside the quotes that open a file for writing raw bytes.
3. with open('data.bin', 'rb') as f: header = f.read(4) print(header)
Hint: Type the file method that takes a number of bytes to fetch.
4. with open('data.bin', 'rb') as f: data = f.read() print(isinstance(data, bytes))
Hint: Type the data type that a binary-mode read returns instead of str.
5. with open('data.bin', 'rb') as f: chunk = f.read(8) print(len(chunk))
Hint: Type the built-in function that opens a file and returns a file object.

Spot the Error

Question 1
A f = open('data.bin', 'rb')
B data = f.read[4]
C print(data)
D f.close()
Line B — read is a method, so the number of bytes goes in round brackets: f.read(4). Square brackets are for indexing, not for calling a method.
Question 2
A f = open('out.bin', 'wb')
B data = bytes([72, 105])
C f.write(str(data))
D f.close()
Line C — A file opened with 'wb' only accepts bytes. str(data) turns it into text, which raises a TypeError. It should be f.write(data).
Question 3
A with open('a.png', 'rb') as f:
B header = f.read(8)
C print(header + '!')
D print(type(header))
Line C — header is bytes, and bytes cannot be joined to a str. Use a bytes literal instead: header + b'!'.
Question 4
A text = "Hello"
B data = text.decode()
C f = open('msg.bin', 'wb')
D f.write(data)
Line B — A str is turned into bytes with encode(). decode() goes the other way, from bytes to str, and str has no decode method. It should be text.encode().
Question 5
A with open('d.bin', 'rb') as f:
B chunk = f.read(16)
C while chunk:
D print(chunk.length)
E chunk = f.read(16)
Line D — bytes objects have no .length attribute. Use the len() function to count the bytes: print(len(chunk)).

Student with a class code? Join your class →