You are viewing a free preview of this lesson.
Subscribe to unlock all 10 lessons in this course and every other course on LearningBro.
This lesson covers file processing — reading from and writing to files — which is an essential skill for A-Level Computer Science. Programs need to persist data beyond their execution time, and files provide a way to store and retrieve data permanently.
Data stored in variables is volatile — it is lost when the program terminates. Files provide persistent storage, allowing data to be:
| Storage Type | Volatile? | Example |
|---|---|---|
| Variables (RAM) | Yes — lost when program ends | A score counter in a game |
| Files (Disk) | No — persists after program ends | A saved game file, a CSV of student records |
| Database | No — persists and supports queries | A relational database of customers |
The basic file operations are:
| Operation | Description |
|---|---|
| Open | Prepares the file for reading or writing. |
| Read | Retrieves data from the file. |
| Write | Stores data to the file (overwriting existing content). |
| Append | Adds data to the end of the file without overwriting. |
| Close | Releases the file, ensuring all data is saved. |
| Mode | Description |
|---|---|
"r" | Read — opens for reading (file must exist). |
"w" | Write — opens for writing (creates file or overwrites existing). |
"a" | Append — opens for appending (creates file if it does not exist). |
"r+" | Read and write — file must exist. |
file = OPEN("students.txt", "r")
WHILE NOT file.endOfFile()
line = file.readLine()
OUTPUT line
END WHILE
file.close()
# Method 1: Read all content at once
with open("students.txt", "r") as file:
content = file.read()
print(content)
# Method 2: Read line by line
with open("students.txt", "r") as file:
for line in file:
print(line.strip())
# Method 3: Read all lines into a list
with open("students.txt", "r") as file:
lines = file.readlines()
for line in lines:
print(line.strip())
with StatementThe with statement in Python automatically closes the file when the block ends, even if an exception occurs. This is the recommended approach.
# Without 'with' — you must remember to close
file = open("data.txt", "r")
content = file.read()
file.close()
# With 'with' — file is closed automatically
with open("data.txt", "r") as file:
content = file.read()
# File is automatically closed here
Exam Tip: Always mention closing files in exam answers. If you use Python's
withstatement, explain that it handles closing automatically. Forgetting to close a file can lead to data loss or corruption.
file = OPEN("output.txt", "w")
file.writeLine("Name,Score")
file.writeLine("Alice,85")
file.writeLine("Bob,72")
file.close()
with open("output.txt", "w") as file:
file.write("Name,Score\n")
file.write("Alice,85\n")
file.write("Bob,72\n")
with open("log.txt", "a") as file:
file.write("New log entry\n")
Exam Tip: Be careful with the difference between
"w"(write) and"a"(append). Using"w"will overwrite all existing content. Using"a"adds to the end. This is a common source of data loss.
Subscribe to continue reading
Get full access to this lesson and all 10 lessons in this course.