First exams 2027 · SL & HL

B2.5.1 · File processing

File processing

Everything a program holds in a variable disappears the moment the program closes. A file is how a program keeps something around anyway, on disk, for the next time it runs.

Core for SL & HL Typically 3–6 marks · AO3 Command term: construct

Explainer

Every variable a program uses lives in memory (RAM) only while the program is running. Close the program, or switch the computer off, and every value in every variable is gone. If a program needs to remember something, a high score, a saved name, a log of what happened, it has to write that information somewhere that survives after the program stops: a file, stored on disk.

Df

A sequential file is a file whose data is accessed in order, one record after another, starting from the beginning. New records are added onto the end. There's no jumping straight to record 50 the way there is with an array index, getting there means reading through everything before it first.

!

Sequential access sounds like a limitation, and for looking something up quickly it is, but it's exactly right for the way most files actually get used: a log that only ever gets read from the start, a batch of records processed one after another overnight, an archive that's written once and rarely reopened at all.

Explainer

Three modes, three different intentions

Opening a file isn't just "I want this file", it's a statement of what you intend to do with it. Python's open() takes the filename and a mode string as its two arguments.

ModeWhat it doesReal example
"w" writeCreates the file if it doesn't exist yet. If it does exist, its old contents are erased the moment the file is opened, before anything is even written.Resetting a save file to start a new game
"r" readOpens the file to read its existing contents. If the file doesn't exist, this raises an error rather than creating one.Loading a player's saved settings when an app starts up
"a" appendCreates the file if it doesn't exist. If it does, new data is added onto the end, the existing contents are left completely alone.Adding one more line to a running system log file

Python's default mode, if none is given, is "r": open("scores.txt") and open("scores.txt", "r") do exactly the same thing.

Write ("w")

The one real trap: opening an existing file in write mode is destructive the instant it's opened.

  • Right tool for: starting completely fresh, a new save file, a report that should only ever show today's data
Append ("a")

Never erases: every open in append mode picks up exactly where the file left off.

  • Right tool for: a growing log, a running history, anything that should keep accumulating across many separate runs of the program

Explainer

Open, do the work, close

Every file-processing operation follows the same shape, and every one of these four steps is something a marker can specifically check your code for.

StepWriting to a fileReading from a file
1. Openf = open("log.txt", "a")f = open("log.txt", "r")
2. Do the workf.write("a new line\n")f.read(), or f.readline() in a loop, or for line in f:
3. Closef.close()f.close()
!

Closing a file isn't a formality. Until close() is called, some of what was written might still be sitting in a buffer rather than actually saved to disk, and other programs (or another part of your own program) might not be able to open the file at all while it's still held open. Every file you open should be closed once you're done with it.

Worked example

Constructing a program that remembers your name

Cooper is building the sign-in screen for a simple classroom quiz app. The first time it runs on a computer, it should ask for the student's name and save it. Every time after that, it should already know who's using it, without asking again.

1

Decide what tells the two cases apart. The file student.txt either exists (this computer has been used before) or it doesn't (first time). Python's os.path.exists() checks that directly.

2

Branch on that check. If the file exists, open it for reading and load the name. If it doesn't, ask for the name and open a new file for writing to save it.

3

Close the file in both branches. Reading and writing are different operations, but every branch that opens a file has to close it too.

import os

filename = "student.txt"

if os.path.exists(filename):
    f = open(filename, "r")
    name = f.read()
    f.close()
    print("Welcome back, " + name + "!")
else:
    name = input("What's your name? ")
    f = open(filename, "w")
    f.write(name)
    f.close()
    print("Nice to meet you, " + name + "! I'll remember you next time.")

Verified against real Python: the first run asks for a name and writes it; every run after that, with the exact same code and no changes at all, skips straight to the welcome-back message.

Same code, two different runs Step 1 of 2

Run 1, on a brand-new computer. student.txt doesn't exist yet, so os.path.exists(filename) is False. The program falls into the else branch: it asks Cooper for his name, opens student.txt in write mode (creating it), writes his name into it, and closes it.

What's your name? Cooper Nice to meet you, Cooper! I'll remember you next time. student.txt now contains: Cooper

Run 2, same computer, program closed and reopened. student.txt exists now, so os.path.exists(filename) is True. The program takes the if branch instead: it opens the file in read mode, reads Cooper's name straight out of it, and greets him immediately, with no question asked at all.

Welcome back, Cooper! student.txt is unchanged: Cooper

Try it yourself

Run it more than once

This console keeps a small saved file for as long as this page stays open, exactly like a real program's file would stay on a real disk between runs. Press Run once, then press it again without changing anything, and watch the log grow instead of starting over. Reloading the page clears it, the same way turning a computer's storage into a blank drive would.

main.py
Loading Python engine…
Output
Press Run to see the output here.

Running in offline fallback mode. The full Python engine couldn't load (blocked school network, or no internet), so this console switched automatically to a small hand-written engine. It has its own simulated file system, files behave exactly as described on this page, but it isn't full Python.

Notice each line prints with an extra blank line underneath it. That's not a bug, readline() and the for line in f: loop both keep the \n that was written into the file as part of the line, and print() adds its own newline on top of that. Real Python does exactly the same thing.

Check your understanding

A program keeps a running history of every quiz score a student has ever earned, in a file called scores.txt. Which mode should it use to open the file each time it adds a new score?

Common mistake

Markers reported this pattern when candidates wrote code to process an entire file

RecurringA common error is failing to recognise that reading a file needs a loop to reach every line, not a single read. Code that calls readline() once, or forgets the loop condition entirely, only ever sees the first record and silently misses everything after it, with no error at all to reveal the mistake.

✕

Only the first line: f = open("scores.txt", "r")
line = f.readline()
print(line)
f.close()
This runs without any error, and looks like it's "reading the file", but it only ever sees whatever is on the very first line, every other score in the file is silently ignored.

✓

Every line: f = open("scores.txt", "r")
line = f.readline()
while line != "":
    print(line)
    line = f.readline()
f.close()
The loop keeps calling readline() and keeps checking the result, right up until it returns an empty string, which is exactly how Python signals that the end of the file has been reached.

Revision questions

Past-paper style. Write an answer, then reveal the marking points and mark yourself.

Question 1 · 3 marks

[3]State the mode that open() should use for each of the following: starting a new save file that must not contain any old data, adding one more entry to an existing activity log, and loading a list of previously saved usernames.

Question 2 · 4 marks

[4]Construct code that opens a file called diary.txt in append mode, writes a single new line of text to it, and closes the file. Your code should work correctly whether or not the file already exists.

Question 3 · 5 marks

[5]Construct code that reads every line from a file called attendance.txt and counts how many lines contain the word "absent". Your code must correctly process every line in the file, however many lines it contains.

Flashcards

Active recall for this statement’s key terms.