This note is from 8 months ago. You're viewing content from a previous lesson.
This section develops students’ ability to construct programs that perform file-processing operations, an essential skill for handling external data. Students will learn how to manipulate text files by opening them in various modes—such as read, write, and append—and how to correctly read from, write to, and close files after operations are completed. Depending on the programming language, students will use appropriate tools such as `open()`, `read()`, and `write()` in Python, or classes like `Scanner`, `FileWriter`, and `BufferedReader` in Java, to build practical and efficient file-handling solutions.
B2.5.1 Construct code to perform file-processing operations.
We have an absent student today, here are notes for him:
File processing allows a program to persist data across executions by reading from and writing to files. This is fundamental in real-world applications such as logging, saving settings, or storing user data.
The IB command term here is Construct. That means you must be able to write working code that performs file operations, not just describe them. A weak answer only explains what might happen; a strong answer shows correct code that opens, reads, writes, or appends to files — and then closes them properly.
When Python reads a file, it does so from the beginning (top) to the end (bottom) in the exact order the data appears. In proper terminology:
File input is sequential by default.
The file’s read pointer starts at the top (byte position 0).
Each read operation advances the pointer downward through the file until the end is reached.
This is why the order of data in a file matters, and why repeated reads will eventually return nothing once the pointer has reached the bottom.
| Mode | Description |
|---|---|
'r' |
Read (file must already exist) |
'w' |
Write (overwrites existing file or creates new) |
'a' |
Append (adds data to the end, creates file if missing) |
'w' mode)Task: Write three names into a file called names.txt, overwriting any existing content.
file = open("names.txt", "w")
file.write("Alice\n")
file.write("Bob\n")
file.write("Charlie\n")
file.close()
Explanation:
Opens names.txt in write mode.
Overwrites any existing content.
Writes three lines.
Closes the file.
'r' mode)Task: Read and print each line from names.txt.
file = open("names.txt", "r")
for line in file:
print(line.strip())
file.close()
Explanation:
Opens file in read mode.
Iterates through each line from top to bottom in sequence.
.strip() removes the newline character.
Closes the file.
'a' mode)Task: Add two more names to the existing file.
file = open("names.txt", "a")
file.write("Diana\n")
file.write("Eli\n")
file.close()
Explanation:
Opens file in append mode.
Adds new lines to the end of the file without deleting the original content.
Closes the file.
.readline() and .read()Task: Show different ways to read content from a file.
file = open("names.txt", "r")
# Read first line only
first = file.readline()
print("First line:", first.strip())
# Read the rest of the file (from the pointer’s current position to the end)
rest = file.read()
print("Rest of file:\n", rest.strip())
file.close()
Explanation:
.readline() reads a single line (starting at the current pointer).
.read() reads everything else sequentially from the current pointer to the end.
Both respect the top-to-bottom order of the file.
Always close files using .close().
Even better: use with — this automatically closes the file, even if an error occurs:
with open("names.txt", "r") as file:
data = file.read()
'w' → write (erase old content, create new if missing).
'r' → read (file must exist, read sequentially from top to bottom).
'a' → append (add to end without erasing).
Always close the file.