Community resourceWorksheet

J277 2.2.3 File handling

Part 2 of 5 · J277 2.2.3 · Applied programming

Persistent storage taught in OCR Exam Reference Language first, then the same open, read, write and close operations in runnable Python.

Students will:

  • explain why a file keeps data that a variable does not
  • use open, readLine, writeLine and close in the right order
  • read every line of a file using endOfFile in a loop
  • carry the same operations across into Python, with the mode strings supplied
  • write a file-handling procedure that uses the parameters it is given

Inside: 8 explanation cells, 2 runnable Python tasks, 1 multiple-choice question, 3 fill-in-the-blanks cells and 1 written answer. 21 marks, about 45 minutes.

Series: J277 2.2.3 · Applied programming, part 2 of 5.

Shared by Coding PathwayVerified teacher

  • 15 cells
  • About 45 minutes
  • CC BY-SA 4.0
  • Shared 17 Aug 2026
  • Updated 9 Sept 2026

Preview

The whole resource, exactly as a class sees it. Answers and marking are held back.

File handling

Variables normally lose their values when a program finishes. A file allows data to be kept so that it can be used later. This is called persistent storage.

OCR requires four basic file-handling operations:

  1. Open the required file.
  2. Read existing data or write new data.
  3. Close the file when it is no longer needed.

The opened file is stored in a variable, such as scoresFile. Later instructions use that variable to refer to the same file.

The open, use and close file-handling lifecycle A program opens a named file and keeps a reference to it in a variable. It reads or writes data using that reference, then closes the file. Data written to the file remains available after the program ends. 1. Open name the file store its reference 2. Use readLine() or writeLine(data) 3. Close finish using the file Data remains persistent

The middle stage depends on the task: a program may read existing data, write new data, or do both. It must still close the file after its final operation.

Multiple choice1 mark

Why might a program store results in a file rather than only in a variable?

  • AA file prevents every possible input error
  • BA file automatically sorts all results
  • CA file keeps the results after the program has ended
  • DA file removes the need for an algorithm

OCR Exam Reference Language

OCR examination questions may present file handling like this:

scoresFile = open("scores.txt")
score = scoresFile.readLine()
scoresFile.writeLine("18")
scoresFile.close()
InstructionPurpose
open("scores.txt")Opens the named file.
.readLine()Reads the next line from the file.
.writeLine(data)Writes data as a line at the end of the file.
.close()Closes the file after use.

The variable before the dot must refer to the file that was opened. For example, scoresFile.readLine() reads from the file stored in scoresFile.

Fill in the blanks3 marks
Choose from the answer bank to complete the OCR Exam Reference Language sequence. The algorithm must open scores.txt, read its first line, then close the file: scoresFile = label 1 firstScore = scoresFile. label 2 scoresFile. label 3
  • readLine()
  • close()
  • open("scores.txt")
  • writeLine()
  • endOfFile()

Reading every line

readLine() reads the next line. To process an entire file, an algorithm can repeat until it reaches the end:

namesFile = open("names.txt")

while NOT namesFile.endOfFile()
    name = namesFile.readLine()
    print(name)
endwhile

namesFile.close()

The condition is checked before another line is read. The close operation belongs after the loop because the file is still needed during every repetition.

Fill in the blanks3 marks
Choose from the answer bank to complete the OCR Exam Reference Language algorithm. It must read and display every name, stop at the end of the file, then close it: while NOT attendanceFile. label 1 name = attendanceFile. label 2 print(name) endwhile attendanceFile. label 3
  • close()
  • writeLine(name)
  • readLine()
  • open()
  • endOfFile()

Writing a line

OCR's writeLine() operation writes a line at the end of the file. A short procedure might receive both the data and the filename as parameters:

procedure SaveEntry(entry, filename)
    logFile = open(filename)
    logFile.writeLine(entry)
    logFile.close()
endprocedure

Notice that the procedure uses its parameters. It does not replace them with one fixed filename or one fixed entry. This allows the same procedure to be reused.

Fill in the blanks5 marks
Choose from the answer bank to complete the reusable OCR procedure. It must receive a result and filename, open that file, write the result, then close the file: procedure SaveResult( label 1 , label 2 ) resultFile = label 3 resultFile. label 4 resultFile. label 5 endprocedure
  • filename
  • close()
  • result
  • readLine()
  • open(filename)
  • open("results.txt")
  • writeLine(result)

The same ideas in Python

Python is the high-level language used in these worksheets. Python needs a mode such as "w" or "r" when opening a file, so those mode strings appear in the runnable example below. They are supplied as Python implementation details; the OCR specification focus remains opening, reading, writing and closing.

Before running the program, predict the displayed message. Follow the file variable through the write, close, reopen, read and final close operations.

Worked example
# Python example: write a message, then read it back.
message = "Revision starts at 4 pm"

file = open("message.txt", "w")  # Python write mode is supplied.
file.write(message)
file.close()

file = open("message.txt", "r")  # Python read mode is supplied.
saved_message = file.read()
file.close()

print(saved_message)

Supported Python practice

Complete the four file operations in the supplied program:

  1. Write the value of reminder to the already-open file.
  2. Close that file.
  3. After the supplied reopening statement, read all its text into saved_text.
  4. Close the file again.

The Python mode strings have already been provided. Your task is to use the same write, close, read and close sequence that the OCR model teaches. The checks will also use a different reminder, so do not type the expected sentence directly into saved_text.

Coding task4 marks
reminder = "Bring calculator"

file = open("reminder.txt", "w")  # Supplied Python opening statement.
# Write reminder to file, then close the file.

file = open("reminder.txt", "r")  # Supplied Python reopening statement.
# Read the complete text into saved_text, then close the file.

Independent exam-style practice

OCR questions may ask you to write or complete a short file-handling subprogram. Use the parameters named in the question, keep the operations in a sensible order and make it clear that the same opened file is written to and closed.

Written answer5 marks

Write a procedure called SaveAttendance that receives name and filename as parameters. It must open the supplied file, write the supplied name as a line and close the file. You may use OCR Exam Reference Language or Python.

Use the two parameters rather than fixed values. Store the opened file in a variable, then use that same variable for writing and closing.

Students type their answer here.

Review

Files provide persistent storage. In OCR Exam Reference Language, remember the sequence open(), read with readLine() or write with writeLine(), then close(). Use endOfFile() when every line must be processed. Keep the opened file in a variable and use that same variable throughout.

Python needs additional mode syntax to run real examples, but do not let those mode letters distract from OCR's assessed ideas. In an examination response, answer the operation the question asks for and use the supplied filename and data rather than hard-coded alternatives.