Open, Read, Write, Close — in OCR ERL and Python
with statement for safe file handlingscores.txt. Program ends → score saved. Next run → score loaded back from file. Persistent data.| Operation | Purpose |
|---|---|
| Open | Connect the program to a file — specifying read or write mode |
| Read | Retrieve data from the file into the program |
| Write | Send data from the program out to the file |
| Close | Disconnect the file — saves changes and frees resources |
open() without a second argument opens in read mode by default. Pass "w" to open for writing.| Mode | ERL | Effect |
|---|---|---|
| read | open("f.txt") | Read existing content |
| write | open("f.txt","w") | Creates/overwrites file |
"w") immediately deletes all existing content — even before you write anything. If the file does not exist, it is created. Use write mode with care.readline() reads one line from the file and moves the read position forward. Each call returns the next line. The line includes the newline character at the end.with statement closes the file automatically when the indented block ends — even if an error occurs. This is safer than calling close() manually.endOfFile() returns True. This works for files of any length — you don't need to know how many lines there are.endOfFile() returns True when the file pointer has passed the last line. The condition NOT endOfFile() means: keep looping while there are still lines to read.
WHILE NOT myFile.endOfFile() DO ... ENDWHILE with readline() inside. This is the expected pattern.write() sends a string to the file. It does not automatically add a newline. You must add "\n" to move to a new line for the next write."\n" to the end of each written value — without it, the next write continues on the same line.str() first.i tracks which array slot to fill next..strip() method in Python removes the trailing \n from each line when reading. In OCR ERL pseudocode you don't need to worry about this — just use readline() directly.open() and the program crashes before close(), the file may be left open and data may be lost or corrupted. The with statement guarantees the file is closed even if an error occurs.| Mode | Meaning | Creates? |
|---|---|---|
| "r" | Read only | No — error if missing |
| "w" | Write (overwrite) | Yes — creates new |
| "a" | Append (add to end) | Yes — creates new |
open("f") for read and open("f","w") for write. Python adds "r" for read and "a" for append. The exam focuses on read and write — know both.| Operation | OCR ERL | Python |
|---|---|---|
| Open read | open("f") | open("f","r") |
| Open write | open("f","w") | open("f","w") |
| Read line | f.readline() | f.readline() |
| End of file | f.endOfFile() | line == "" |
| Write | f.write(s) | f.write(s) |
students.txt (one name per line), prints each name, then writes the message "All students loaded." to a file called log.txt.myFile = open("data.txt", "w")scores.txt, read and print every line until the end of the file, then close it.names.txt, one per line"w" in open — defaults to read mode, writing will error"\n" in write — all names end up on one lineclose() — always required for full marks"\n" and close. Each is one logical step — one mark each.myFile.close() at the end. This is nearly always a mark in the exam — and in real programs, not closing may leave data unsaved or corrupt the filemyFile.close() — it is an explicit OCR spec pointopen("file.txt") (default read) then calling write() — this will cause an error because the file is open in read modeopen("file.txt", "w") — the "w" is requiredmyFile.write("Alice") followed by myFile.write("Bob") — the file will contain "AliceBob" on one line+ "\n" to each write: myFile.write("Alice\n")myFile.write(score) where score is an integer — write() only accepts strings, so this causes a TypeErrormyFile.write(str(score) + "\n")"r" opens for reading; "w" opens for writing and immediately deletes existing contentreadline() reads one line per call. Use WHILE NOT endOfFile() DO ... ENDWHILE to read every line in a file of unknown lengthwrite() accepts strings only. Always add "\n" to move to a new line. Cast numbers with str() before writingwith open(...) as f: pattern — it closes the file automatically even if an error occurs. OCR ERL uses endOfFile(); Python checks line != ""Get the full resource pack at CSZone.co.uk