File Handling (Cambridge International IGCSE Computer Science)
Exam board Cambridge International · IGCSEUpdated 2026-06-11
Spec point — "1 Understand the purpose of storing data in a file to be used by a program 2 Open, close and use a file for reading and writing"
File Handling
What is file handling?
-
File handling is the use of programming techniques to work with information stored in text files
-
Examples of file handing techniques are:
- opening text files
- reading text files
- writing text files
- closing text files
| Concept | OCR exam reference | Python |
|---|---|---|
| Open | OPENFILE "fruit.txt" FOR READ | file = open("fruit.txt","r") |
| Close | CLOSEFILE "fruit.txt" | file.close() |
| Read line | READFILE "fruit.txt", LineOfText | file.readline() |
| Write line | OPENFILE "fruit.txt" FOR WRITE WRITEFILE "fruit.txt", "Oranges" | file = open("fruit.txt","w")file.write("Oranges") |
| Append a file | OPENFILE "fruit.txt" FOR APPEND | file = open("fruit.txt","a") |
Pseudocode example (reading data)
| Employees | Text file |
|---|---|
| Greg Sales 39000 43 Lucy Human resources 26750 28 Jordan Payroll 45000 31 |
Python example (reading data)
| Employees | Text file |
|---|---|
| Greg Sales 39000 43 Lucy Human resources 26750 28 Jordan Payroll 45000 31 |
Pseudocode example (writing new data)
| Employees | Text file |
|---|---|
| Greg Sales 39000 43 Lucy Human resources 26750 28 Jordan Payroll 45000 31 Polly Sales 26000 32 |
Python example (writing new data)
| Employees | Text file |
|---|---|
| Greg Sales 39000 43 Lucy Human resources 26750 28 Jordan Payroll 45000 31 Polly Sales 26000 32 |
Exam Hint When opening files it is really important to make sure you use the correct letter in the open command
- "r" is for reading from a file only
- "w" is for writing to a new file, if the file does not exist it will be created. If a file with the same name exists the contents will be overwritten
- "a" is for writing to the end of an existing file only
Always make a backup of text files you are working with, one mistake and you can lose the contents!
Worked Example Use pseudocode to write an algorithm that does the following :
- Inputs the title and year of a book from the user.
- Permanently stores the book title and year to the existing text file *books.txt [4]
How to answer this question
- Write two input statements (title and year of book)
- Open the file
- Write inputs to file
- Close the file
Final answer: Example answer
Guidance
Final answer: 1 mark Final answer: 1 mark Final answer: 1 mark Final answer: 1 mark
Was this revision note helpful?
Build on this topic