Revise Computing
Revision notesQuestionsFor schools
Log inStart revising free
Revision notesQuestionsFor schoolsLog in
GCSE Computer Science/Paper 2/Additional Programming Techniques

Open, Read, Write, Close

The use of basic file handling operations: Open, Read, Write, Close

2.2.3 Additional Programming Techniques

Practise exam questions

Additional Programming Techniques — Note 3 of 12

why you need to open and close a file as well as complete the techniques listed.

What you need to know

File handling allows programs to read data from and write data to external files. Files must be opened before they can be used and closed afterwards to ensure changes are saved. Files can be opened in read mode (to access existing data) or write mode (to save new data). This is essential for storing data permanently between program runs.

What to know

  • Files must be opened before reading or writing and closed when finished.
  • Open for reading: myFile = open("sample.txt") in OCR pseudocode.
  • Read a line: myFile.readLine() returns the next line in the file.
  • Open for writing: myFile = open("sample.txt") then myFile.writeLine("text").
  • Closing a file with myFile.close() ensures all changes are saved.
  • endOfFile() checks if the end of the file has been reached (useful in loops).
  • newFile("name.txt") creates a new empty file.
  • If a file is not closed, changes may be lost.
  • Python file modes: "r" opens a file for reading; "w" creates a new file or OVERWRITES an existing one.
  • OCR to Python: open("f.txt") → open("f.txt", "r"); readLine() → readline(); writeLine(x) → write(x + "\n"); endOfFile() → line == ""; newFile("f.txt") → open("f.txt", "w").
  • readline() returns an empty string "" when there are no lines left, so while line != "": repeats until the end of the file.

Exam technique

  • Tip:A common question asks you to write a procedure that writes to a file. The procedure takes data and filename as PARAMETERS — use these parameters, don't ask for new input inside.
  • Mistake:Opening an existing file with "w" wipes everything already in it.
  • Example:procedure SaveLogs(data, filename) → open(filename), writeLine(data), close(). Must use the parameters passed in.

Code examples

File handling in Python
python
# Writing to a file
f = open("scores.txt", "w")
f.write("Alice,85\n")
f.write("Bob,92\n")
f.close()

# Reading from a file
f = open("scores.txt", "r")
for line in f:
    print(line.strip())
f.close()
Open and write to a file
python
file = open("data.txt", "w")  # "w" creates the file, or overwrites it
file.write("Line 1\n")        # \n moves on to a new line
file.write("Line 2\n")
file.close()                  # always close the file to save it
Read every line until the end of the file
python
file = open("data.txt", "r")  # "r" opens the file for reading
line = file.readline()        # read the first line

while line != "":             # readline() returns "" at the end of the file
    print(line.strip())       # strip() removes the newline at the end
    line = file.readline()    # read the next line

file.close()

Ready to test yourself?

Practise 2.2.3 questions
← Previous noteslicing2.2.3 Additional Programming TechniquesNext note →Records to store data2.2.3 Additional Programming Techniques

Back to Additional Programming Techniques

Revise another OCR GCSE topic: Programming Fundamentals

Additional Programming Techniques

Note 3 of 12

  1. 1Concatenation
  2. 2slicing
  3. 3Open, Read, Write, Close
  4. 4Records to store data
  5. 5SQL to search for data
  6. 6one-dimensional (1D)
  7. 7two-dimensional arrays(2D)
  8. 8Sub programs (functions and procedures) to produce structured code
  9. 9local variables
  10. 10global variables
  11. 11arrays
  12. 12Random number generation
Topic overview

© 2026 Revise Computing · revisecomputing.co.uk

Privacy noticeTermsContact

Revise Computing is an independent revision resource. It is not affiliated with, or endorsed by, OCR (Oxford Cambridge and RSA Examinations).