19-text-file-handling.py
python-programming / further-programming-techniques  ·  Lesson 20
Lesson 20 — Further programming techniques

Text File Handling

Open, read, write and close text files.

Files let data remain available after a program ends. The essential operations are open, read, write and close.

Predict

Read the code first. Do not run it yet.

text-file-handling-predict.py
file = open("message.txt", "w")
file.write("Hello")
file.close()
Predict before running
  • Which mode opens the file for writing?
  • What data is written?
  • Why should the file be closed?

Investigate

Use this code to explain exactly how the program works.

text-file-handling-investigate.py
file = open("message.txt", "r")
text = file.read()
file.close()
print(text)
Investigate the code
  • Which operation opens the file?
  • Which operation reads its contents?
  • In what order are read and close performed?

Writing to a file

write_file.py
file = open("names.txt", "w")
file.write("Ava\n")
file.write("Ben\n")
file.close()
How it works
  • "w" opens the file for writing.
  • write() sends text to the file.
  • \n starts a new line.
  • close() closes the file when the program has finished with it.

Reading from a file

read_file.py
file = open("names.txt", "r")
first = file.readline().strip()
second = file.readline().strip()
file.close()
print(first, second)
Output
Ava
Ben
Core file operationsRemember the sequence: open → read or write → close.

Self-study guide

Text-file handling lets a program store data beyond the lifetime of its variables. OCR requires the basic operations Open, Read, Write and Close. Python uses a file object created by open(), followed by methods such as .read(), .readline(), .write() and .close().

Python file modes matter. Opening with "r" reads an existing file; opening with "w" writes and replaces previous contents. Python .write() does not automatically add a new line, so include "\n" when each value should be on a separate line.

OCR focusPractically use Open, Read, Write and Close. Recognise OCR .readLine() / .writeLine() and translate them into the Python file operations used in class.

OCR Exam Reference Language ↔ Python

OCR Exam Reference Language / termPythonWhat to remember
myFile = open("sample.txt")file = open("sample.txt", "r")Open a file and store the handle/object.
myFile.readLine()file.readline()Read the next line.
myFile.writeLine("text")file.write("text\n")Python .write() needs \n if you want a line break.
myFile.close()file.close()Close the file.
myFile.endOfFile()no identical method used herePython code often reads a known number of lines or iterates over file lines instead.
Read both formsIn an examination you may see OCR Exam Reference Language even when you have learned the idea in Python. Translate the structure, not just the symbols.

Worked example

This example writes three lines, closes the file, then reopens and reads the saved text.

lesson-20-worked.py
file = open("notes.txt", "w")
file.write("Variables\n")
file.write("Selection\n")
file.write("Iteration\n")
file.close()

file = open("notes.txt", "r")
data = file.read()
file.close()

print(data)
Output
Variables
Selection
Iteration
Work through it line by line
  • Write mode creates/replaces the file contents.
  • Each write() receives a string.
  • \n creates a newline between entries.
  • The file is closed after writing.
  • Read mode reopens the stored file, .read() retrieves the text, and the file is closed again.

Another example

Numeric values must be converted to text before using .write().

lesson-20-example-2.py
score = 42
file = open("score.txt", "w")
file.write(str(score))
file.close()
Why this example matters
  • score is an integer.
  • str(score) converts it to text suitable for writing.

Common mistakes

Avoid these errors
  • Forgetting to close the file.
  • Writing an integer directly with .write() instead of converting it to a string.
  • Assuming Python .write() adds a newline automatically.
  • Opening in "w" mode when you intend to preserve existing contents.
Exam-safe warningOCR .writeLine() and Python .write() are not identical: Python needs \n when you want a newline.

Exam-ready checklist

You should now be able to
  • Open a file for reading or writing.
  • Read text using .read() / .readline() and write using .write().
  • Close files correctly.
  • Translate basic OCR file operations to Python.
  • Convert numeric data to text before writing.

What you have learned

  • Use open() before accessing a file.
  • Use read() or readline() to read.
  • Use write() to write text.
  • Use close() when finished.