← IB Computer Science

File Handling

Data stored in variables and lists disappears when a program closes. Files let a program save data so it lasts, and load it back later. This unit (B2.5) covers why programs use files, how sequential files work, and how to read from and write to files, including the difference between overwriting and appending.

Slide 1 / 4

Why programs use files

Memory is lost — files persist In memory (RAM) variables, lists while the program runs program closes all data is lost (volatile) write read In a file (disk) data still there next time high scores, settings, logs, records
  • While a program runs it holds data in variables and lists in memory, but when the program closes all of that data is lost.
  • Writing data to a file lets a program store (persist) it, so the data is still there the next time the program runs, or for another program to use.
  • Common examples include saving high scores, user settings, log files, and records that need to be kept long term.
  • Reading from a file loads saved data back into the program so it can be used or updated.

Check you've got it

0/4

1. Why do programs write data to files instead of only keeping it in variables?

2. How is data read from a sequential file?

3. A program opens an existing file in append mode ("a" in Python) and writes to it. What happens to the data that was already in the file?

4. Why should a program close a file after it has finished writing to it?

Free revision content. Multiple-choice answers are marked instantly. Written answers are marked by AI on /free-mark.