Files & Data

💻 Computer Science Years 9–11 Foundation

Storing data permanently in text files: open/read/write/close, file modes, and how records and fields structure the data.

Open this lesson, with practice and a printable worksheet →

Turn a program off and everything it was holding in memory vanishes. So how does a game remember your high score next week, or a register keep last term's marks? The answer is files — data written to disk so it survives long after the program closes.

Why this matters: Variables live in memory and die when the program ends; that is volatile storage. Files are persistent — they stay. Reading and writing text files is how programs load settings, save progress and swap data with other systems. Exam tasks routinely ask you to read a file line by line and process each record.
marks.txt — one record per line Aisha,72 Omar,64 Leah,88 name , mark (two fields)
A text file is a stack of records; each line splits into fields.

Memory vs the disk

MEMORYscore = 42volatile — wipedFILEscore = 42persistent — kept
Memory is a whiteboard (wiped); a file is a notebook (kept).

Think of memory as a whiteboard and a file as a notebook. Whatever you scribble on the whiteboard is wiped the moment you leave the room (the program ends). Write it in the notebook and it is still there tomorrow. Programs constantly copy data from notebook to whiteboard (reading) and back again (writing).

The three-step dance: open, use, close

OPENREAD /WRITECLOSEalways the same rhythmsaves & frees the file
Every file follows the same order: open → use → close.

Working with a file always follows the same rhythm. You open it in a mode, you read or write, then you close it so the data is safely saved and the file is freed for others.

ModePurpose
Read ("r")Open an existing file to read its contents
Write ("w")Create/overwrite a file with new contents
Append ("a")Add to the end, keeping what is already there

Records and fields

one record, split on the commaAisha,72Aisha72
A comma is the separator: one record → the name field and the mark field.

Structured text files store one record per line — all the data about one thing, such as one pupil. Each record is split into fields by a separator, usually a comma (a CSV file). So Aisha,72 is one record with two fields: a name and a mark.

Worked example — reading and processing.
open "marks.txt" for read
while not end of file
    line = readline()          # "Aisha,72"
    parts = line.split(",")    # ["Aisha","72"]
    name = parts[0]
    mark = parts[1]
    print(name + " scored " + mark)
endwhile
close file
Read each line, split it into fields on the comma, then use each field.
Common mistake: opening a file in write mode ("w") when you meant to add to it. Write mode overwrites the whole file, destroying the old contents. Use append mode ("a") to keep existing data and add to the end.

Tip: always close a file when you are done. Data written to a file may sit in a buffer and only be fully saved when the file is closed — forget to close it and you can lose the last records.

Get the printable worksheet →
← Functions & Procedures → Databases & SQL
← Sasta Basta home All subjects