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.
file = open("message.txt", "w")
file.write("Hello")
file.close()- 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.
file = open("message.txt", "r")
text = file.read()
file.close()
print(text)- Which operation opens the file?
- Which operation reads its contents?
- In what order are read and close performed?
Writing to a file
file = open("names.txt", "w")
file.write("Ava\n")
file.write("Ben\n")
file.close()"w"opens the file for writing.write()sends text to the file.\nstarts a new line.close()closes the file when the program has finished with it.
Reading from a file
file = open("names.txt", "r")
first = file.readline().strip()
second = file.readline().strip()
file.close()
print(first, second)Ava Ben
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.
.readLine() / .writeLine() and translate them into the Python file operations used in class.OCR Exam Reference Language ↔ Python
| OCR Exam Reference Language / term | Python | What 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 here | Python code often reads a known number of lines or iterates over file lines instead. |
Worked example
This example writes three lines, closes the file, then reopens and reads the saved text.
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)Variables Selection Iteration
- Write mode creates/replaces the file contents.
- Each
write()receives a string. \ncreates 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().
score = 42
file = open("score.txt", "w")
file.write(str(score))
file.close()scoreis an integer.str(score)converts it to text suitable for writing.
Common mistakes
- 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.
.writeLine() and Python .write() are not identical: Python needs \n when you want a newline.Exam-ready checklist
- 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()orreadline()to read. - Use
write()to write text. - Use
close()when finished.