Key Idea: A file keeps data after the program ends. Everything about file handling follows from two facts: writes are buffered, and the file may not be what the program expects.
Paper 1
- Short definitions and a trace.
- State, identify, outline — precise terms.
Paper 2
- Write or correct Python for a scenario — your paper is the Python version, option B.
- Construct, determine, suggest.
Both
- Trace tables and justified choices earn method marks even when the final answer slips.
📂 The operations
Open, read or write, close
- Closing flushes the buffer — without it the most recent writes can be lost entirely
- Closing also releases resources; a loop that never closes exhausts them
- Opening for WRITING discards the existing contents the moment it opens
- Opening for APPENDING preserves them — which is why a log must be opened that way
- The buffer exists because writing to disc one byte at a time would be far too slow
For fixed-length records: byte position = record number × record length record number = byte position ÷ record length Variable-length records make direct access impossible without an index.
🎯 Access methods
| Sequential | Direct | |
|---|---|---|
| Reads | From the start, in order | Jumps to a calculated position |
| Needs | Nothing special | Fixed-length records or an index |
| Cost of record n | Pass everything before it | Constant, whatever n is |
| Suits | Processing every record | Looking one record up |
| Text file | Binary file | |
|---|---|---|
| Readable | In any editor | Only with the exact layout |
| Size | Larger | Compact |
| Loading | Needs conversion | No conversion |
| Diagnosing a fault | By eye | Requires a tool |
🔐 Writing safely
Important: A failure part-way leaves neither version complete. Write to a temporary file, then rename it over the original — a rename either happens or it does not, so there is no half-written state.
Reading defensively
- Never assume a file is correctly formatted — it may be hand-edited, truncated, or from an older version
- Validate each record as it is read, so one bad line is reported and skipped
- A comma inside a value breaks a comma-separated file: quote it or choose another separator
- A missing file, a locked file and a full disc are all normal — handle them, do not assume them away
📝 Exam-style questions
A file stores 12 000 records of 96 bytes each, with a 512-byte header. Calculate the file size in kilobytes (1 KB = 1024 bytes), and the byte position at which record 500 begins (records numbered from 0, immediately after the header).
🔒 Model answer plan
See the mark-by-mark plan — for / against / judgement, with marking guidance — in study mode.
A program appends a line to a log file inside a loop and the computer loses power. Some lines written seconds earlier are missing. Explain why, and state what would prevent it.
🔒 Model answer plan
See the mark-by-mark plan — for / against / judgement, with marking guidance — in study mode.
✅ Quick check
Cover the answers.
What does closing a file do beyond ending access? Flushes the buffer to disc and releases the resources the open file holds.
Which open mode destroys the existing contents? Opening for writing — and it does so the moment it opens, before anything is written.
Why does direct access need fixed-length records? The position is calculated as record number × record length — impossible if lengths vary.
How do you replace a file safely? Write a temporary file, then rename it over the original — a rename cannot half-happen.
Why is a text file often preferred despite being larger? It is readable in any editor, so a fault can be diagnosed by eye and the data survives the program that wrote it.
Exam tips
- File-size questions are arithmetic with units — read whether bytes, KB or MB is wanted.
- Include the header in both the size and the position calculation.
- Records numbered from 0: record n starts at header + n × length.
- Name the BUFFER when explaining lost writes — that is the marked point.
- Write mode destroys; append mode preserves. Say which a log needs.
- Never overwrite in place — temporary file, then rename.