Reading and writing binary files

Tip: Choose Save as PDF in the print dialog to download. Use A4 paper size.

Reading and writing binary files

Name:
Date:
Class:

Quick Questions

Shade in the A or B circle for each question.

1. Which line opens a file so read() returns raw bytes?
A open('data.bin', 'rb')
B open('data.bin', 'r')
2. Which mode do you use to write raw bytes to a file?
A 'wb'
B 'w'
3. What type does f.read() return when f was opened with 'rb'?
A bytes
B str
4. Which write call works on a file opened with 'wb'?
A f.write('Hi')
B f.write(b'Hi')
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

Match the Pairs

Draw a line connecting each item on the left to its match on the right.

Left columnRight column
1f = open('data.bin', 'rb') AA str is written to a file in 'wb' mode. Binary mode needs bytes, so use b'Hello' or 'Hello'.encode().
2f = open('copy.bin', 'wb') Bf.write(b'AB')
3Reading exactly 4 bytes from a file opened in binary mode COpens the file in binary read mode so reading gives raw bytes instead of text
4Binary data is type bytes, not str, so a b prefix is used for a bytes literal DThe file is opened in text mode 'r', so read(4) gives 4 characters as str, not 4 raw bytes. Use 'rb'.
5f = open('data.bin', 'wb') f.write('Hello') EOpens the file in binary write mode so you can write raw bytes to it
6f = open('data.bin', 'r') data = f.read(4) Fchunk = f.read(4)

Definitions

open('data.bin', 'rb')

Fill in the Blanks

Complete each line of code by filling in the missing parts.

1.
with open('data.bin', '__') as f: data = f.read() print(type(data))
2.
with open('copy.bin', '__') as f: f.write(bytes([65, 66, 67]))
3.
with open('data.bin', 'rb') as f: header = f.____(4) print(header)
4.
with open('data.bin', 'rb') as f: data = f.read() print(isinstance(data, _____))
5.
with ____('data.bin', 'rb') as f: chunk = f.read(8) print(len(chunk))

Spot the Error

Shade in the letter of the line that contains an error.

Question 1
A f = open('data.bin', 'rb')
B data = f.read[4]
C print(data)
D f.close()
Question 2
A f = open('out.bin', 'wb')
B data = bytes([72, 105])
C f.write(str(data))
D f.close()
Question 3
A with open('a.png', 'rb') as f:
B header = f.read(8)
C print(header + '!')
D print(type(header))
Question 4
A text = "Hello"
B data = text.decode()
C f = open('msg.bin', 'wb')
D f.write(data)
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)

Answer Key

Reading and writing binary files
Section A: Quick Questions
Q1
A — open('data.bin', 'rb')
Q2
A — 'wb'
Q3
A — bytes
Q4
B — f.write(b'Hi')
Q5
B — The first 4 bytes of the file
Section B: Match the Pairs
Q1
f = open('data.bin', 'rb') → Opens the file in binary read mode so reading gives raw bytes instead of text
Q2
f = open('copy.bin', 'wb') → Opens the file in binary write mode so you can write raw bytes to it
Q3
Reading exactly 4 bytes from a file opened in binary mode → chunk = f.read(4)
Q4
Binary data is type bytes, not str, so a b prefix is used for a bytes literal → f.write(b'AB')
Q5
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().
Q6
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'.
Section C: Definitions
Q1
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.
Section D: Fill in the Blanks
Q1
with open('data.bin', 'rb') as f: data = f.read() print(type(data))
Q2
with open('copy.bin', 'wb') as f: f.write(bytes([65, 66, 67]))
Q3
with open('data.bin', 'rb') as f: header = f.read(4) print(header)
Q4
with open('data.bin', 'rb') as f: data = f.read() print(isinstance(data, bytes))
Q5
with open('data.bin', 'rb') as f: chunk = f.read(8) print(len(chunk))
Section E: Spot the Error
Q1
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.
Q2
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).
Q3
C — header is bytes, and bytes cannot be joined to a str. Use a bytes literal instead: header + b'!'.
Q4
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().
Q5
D — bytes objects have no .length attribute. Use the len() function to count the bytes: print(len(chunk)).