OCR · GCSE · Computer Science
Course version: J277 · Version from Sept 2022
The use of basic file handling operations Open Read Write Close
Full specification wording
Additional programming techniques: The use of basic file handling operations Open Read Write Close
File handling lets a program keep data after it stops running.
Checked against the OCR J277 specification on .
- Point 2 of 7
- 4 key terms
Study notes
From the checked teaching notes for this specification point.
The idea
File handling lets a program keep data after it stops running. You open a file to create a connection and choose a mode: read ('r') to fetch existing data, write ('w') to replace or create content, or append ('a') to add to the end. Reading typically uses read(), readline() or readlines() to bring text into a variable. Writing uses write() to send a string to the file. Close() releases the file and flushes buffered data; failing to close can lose data or lock the file. For example, open('scores.txt','w'), write('12\n'), then close() saves one score. Later, open('scores.txt','r'), read the line, convert with int(), and close(). Always match the mode to the task and handle missing files so the program does not crash.
What earns the marks
- Opening a file creates a connection between the program and a named file on secondary storage, and the chosen mode determines whether the program can read, overwrite or append data.
- Read operations retrieve data from an opened file into program variables; read() returns the whole remaining content, readline() returns one line, and readlines() returns a list of lines.
- Write operations send string data to an opened file; in write mode existing content is replaced, while append mode adds new data after existing content.
- Closing a file releases the connection and ensures buffered data is saved; programs should close files after use, often using with open(...) as f: so closing happens automatically.
- File paths and modes must be correct for the intended operation; attempting to read a file that does not exist, or writing to a read-only file, causes an error unless handled.
- Data read from a text file arrives as strings, so numeric processing requires conversion such as int() or float() before calculation.
Common mistakes and how to put them right
Forgetting to close a file after writing, which can leave data unsaved or the file locked.
Put it right: Call close() or use a with block that closes automatically.
Using write mode when append was intended, which erases existing records.
Put it right: Choose 'a' to add data and 'w' only when replacing the whole file is deliberate.
Treating read data as a number without conversion, so arithmetic fails or concatenates strings.
Put it right: Convert with int() or float() before calculating.
Assuming read() returns a list of lines.
Put it right: Use readlines() for a list, or split() the result of read() if needed.
Exam tips
- When asked to complete code, check the mode matches the task: 'r' for reading, 'w' for replacing, 'a' for adding.
- Show the close() call or a with open(...) as f: structure in written answers to demonstrate correct resource management.
- If a question asks for output, trace the file contents after each operation, remembering that write mode clears the file first.
- Use meaningful variable names for file objects and data so your code is easy to follow in an exam response.
Try the lesson
The start of the Additional programming techniques: The use of basic file handling operations Open Read Write Close lesson, made from checked teaching notes for this specification point.
Have a go first
Which file handling operation is used to make a file available so that a program can read data from it or write data to it?
Listen
File handling lets a program keep data after it stops running. You open a file to create a connection and choose a mode: read ('r') to fetch existing data, write ('w') to replace or create content, or append ('a') to add to the end. Reading typically uses read(), readline() or readlines() to bring text into a variable. Writing uses write() to send a string to the file. Close() releases the file and flushes buffered data; failing to close can lose data or lock the file. For example, open('scores.txt','w'), write('12\n'), then close() saves one score. Later, open('scores.txt','r'), read the line, convert with int(), and close(). Always match the mode to the task and handle missing files so the program does not crash. A strong answer earns marks for these points. Opening a file creates a connection between the program and a named file on secondary storage, and the chosen mode determines whether the program can read, overwrite or append data. Read operations retrieve data from an opened file into program variables; read() returns the whole remaining content, readline() returns one line, and readlines() returns a list of lines. Write operations send string data to an opened file; in write mode existing content is replaced, while append mode adds new data after existing content. Closing a file releases the connection and ensures buffered data is saved; programs should close files after use, often using with open(...) as f: so closing happens automatically. A common slip: Forgetting to close a file after writing, which can leave data unsaved or the file locked. Instead, call close() or use a with block that closes automatically. Exam tip: When asked to complete code, check the mode matches the task: 'r' for reading, 'w' for replacing, 'a' for adding.
Key words
Say what each word means before you turn the card.
Open
Try answering aloud or in your head before turning the card.
Read
Try answering aloud or in your head before turning the card.
Write
Try answering aloud or in your head before turning the card.
Close
Try answering aloud or in your head before turning the card.
See every definition
- Open
- Creates a connection between the program and a named file on secondary storage, with the chosen mode determining whether the program can read, overwrite or append data.
- Read
- Retrieves data from an opened file into program variables, where read() returns the whole remaining content, readline() returns one line and readlines() returns a list of lines.
- Write
- Sends string data to an opened file, replacing existing content in write mode while append mode adds new data after existing content.
- Close
- Releases the connection and ensures buffered data is saved, and programs should close files after use, often using with open(...) as f: so closing happens automatically.
There is more to this lesson
The full lesson goes on to check your understanding and help you earn the marks. Create a free account to carry on, with your progress saved.
A week of Premium from your first lesson · Then the free plan continues · No card, no charge
Test your understanding of Additional programming techniques: The use of basic file handling operations Open Read Write Close
Active recall beats re-reading. Take an instant quiz now to test what you’ve learned and lock it into long-term memory.
A week of Premium from your first lesson · Then the free plan continues · No card, no charge