Bob,78).| Mode (Python) | Cambridge description | Typical use |
|---|---|---|
'r' | Read‑only; file must already exist. | Reading existing records. |
'w' | Write‑only; creates a new file or empties an existing one. | Saving fresh data, overwriting old content. |
'a' | Append‑only; writes are added to the end of the file. | Adding log entries. |
'r+' | Read‑write; file must exist. | Updating particular records. |
'w+' | Read‑write; creates a new file or truncates an existing one. | Re‑creating a file with new content. |
'a+' | Read‑append; reads from the start, writes at the end. | Reading a log while adding new entries. |
| Operation | Cambridge keyword | Example (pseudocode) |
|---|---|---|
| Open a file | OPEN | OPEN scoresFile ← "scores.txt", "r" |
| Close a file | CLOSE | CLOSE scoresFile |
| Read a line | READ | READ line FROM scoresFile |
| Write a line | WRITE | WRITE line TO scoresFile |
| Append a line | APPEND | APPEND line TO scoresFile |
| Read entire file | READALL (exam‑style) | READALL data FROM scoresFile |
# Open the file for reading – obtain a file handle called f
f = open('scores.txt', 'r')
# …perform operations…
# Always close when finished
f.close()
In the exam you can replace the above with the Cambridge pseudocode shown in the table.
read() – returns the whole file as one string.readline() – returns the next line (including the newline character).readlines() – returns a list of all lines.with open('scores.txt', 'r') as f: # ← automatically closes
for line in f: # reads one record at a time
process(line) # user‑defined routine
OPEN scoresFile ← "scores.txt", "r"
WHILE NOT EOF(scoresFile) DO
READ line FROM scoresFile
// process the line, e.g. split into name and mark
ENDWHILE
CLOSE scoresFile
write(string) – writes exactly the characters supplied.writelines(list) – writes each element of a list in order.'a' or 'a+' the write position is always at the end of the file.with open('log.txt', 'a') as f:
f.write('User logged in at 12:34')
OPEN logFile ← "log.txt", "a"
APPEND "User logged in at 12:34" TO logFile
CLOSE logFile
This example demonstrates the full cycle: open → read → store in an array → modify → write → close. It follows the exact Cambridge syntax that can be reproduced in Paper 2.
# 1. Read all records into a list of tuples
with open('scores.txt', 'r') as f:
records = [line.strip().split(',') for line in f]
# 2. Convert to a dictionary for easy update (acts like an array of records)
scores = {name: int(mark) for name, mark in records}
# 3. Update a student's mark
scores['Alice'] = 85
# 4. Write the updated data back – overwrite the file
with open('scores.txt', 'w') as f:
for name, mark in scores.items():
f.write(f'{name},{mark}\')
// ---------------------------------------------------
// 1. READ all records into an array called scoresArray
// ---------------------------------------------------
OPEN inFile ← "scores.txt", "r"
SET i ← 0
WHILE NOT EOF(inFile) DO
READ line FROM inFile
SPLIT line BY "," INTO name, mark
SET scoresArray[i] ← name // element 0 = name
SET scoresArray[i+1] ← INTEGER(mark) // element 1 = mark
ADD 2 TO i // each record occupies two cells
ENDWHILE
CLOSE inFile
// ---------------------------------------------------
// 2. UPDATE Alice's mark (example of a write operation)
// ---------------------------------------------------
SET j ← 0
WHILE j < i DO // scan the array
IF scoresArray[j] = "Alice" THEN
SET scoresArray[j+1] ← 85 // new mark
ENDIF
ADD 2 TO j
ENDWHILE
// ---------------------------------------------------
// 3. WRITE the whole array back to the file (overwrite)
// ---------------------------------------------------
OPEN outFile ← "scores.txt", "w"
SET k ← 0
WHILE k < i DO
SET line ← scoresArray[k] + "," + STRING(scoresArray[k+1]) + NEWLINE
WRITE line TO outFile
ADD 2 TO k
ENDWHILE
CLOSE outFile
'r' only when you are sure the file is present, or add a test such as IF FILEEXISTS(...) in your pseudocode.OPEN must have a matching CLOSE. In the exam, write the CLOSE statement on a new line.'w' instead of 'a') – AO2'a' or 'r+' for “add‑only” tasks.'b' suffix. (or NEWLINE in pseudocode) unless the specification says otherwise.scoresFile, studentName, markArray.//) to explain each step.
// Load all student records into an array
OPEN inFile ← "scores.txt", "r"
...
Remember: the exam expects the Cambridge keywords (OPEN, READ, WRITE, APPEND, CLOSE) and a clear, commented structure.
18 → 0001 0010 (binary) → invert → 1110 1101 → add 1 → 1110 1110| Media type | Key parameters | Typical exam calculation |
|---|---|---|
| Audio (PCM) | Sample‑rate, bit‑depth, channels | File size = sample‑rate × bit‑depth × channels × duration ÷ 8 |
| Image (bitmap) | Resolution, colour depth | File size = width × height × colour‑bits ÷ 8 |
Worked example – audio: 5 s of mono sound at 44.1 kHz, 16‑bit.
5 × 44 100 × 16 ÷ 8 = 5 580 000 bytes ≈ 5.3 MB
Worked example – image: 1024 × 768 pixels, 24‑bit colour.
1024 × 768 × 24 ÷ 8 = 2 359 296 bytes ≈ 2.3 MB
| Method | Lossy / Lossless | Typical compression ratio |
|---|---|---|
| JPEG (image) | Lossy | 10 : 1 – 20 : 1 |
| PNG (image) | Lossless | 2 : 1 – 3 : 1 |
| MP3 (audio) | Lossy | 10 : 1 – 12 : 1 |
| FLAC (audio) | Lossless | 2 : 1 – 3 : 1 |
| ZIP (general) | Lossless | 2 : 1 – 5 : 1 |
Exam tip: Choose a lossless method when the original data must be reproduced exactly (e.g., medical images).
// Simple ISR for a keyboard press (pseudocode)
WHEN KEYBOARD_INTERRUPT DO
READ key FROM keyboardPort
STORE key IN buffer
ENDWHEN
Hash | Previous‑Hash | Data | Timestamp. Each block’s hash depends on the previous block, making tampering difficult."data.txt" for appending, add the line "New entry", and close the file.Start → OPEN (choose mode) → Decision: READ or WRITE/APPEND?
If READ: loop READ line until EOF, process data, then CLOSE.
If WRITE/APPEND: prepare data, WRITE or APPEND, then CLOSE → End.
Create an account or Login to take a Quiz
Log in to suggest improvements to this note.
Your generous donation helps us continue providing free Cambridge IGCSE & A-Level resources, past papers, syllabus notes, revision questions, and high-quality online tutoring to students across Kenya.