Reading and Writing Files in Python (GCSE Guide)
June 19, 2026
File handling means reading data from, and writing data to, files stored on a computer. It is a required part of the GCSE Computer Science programming content, and it often appears in the exam. This guide covers the essentials with examples.
Writing to a file
Open a file in write mode ("w") and use write():
file = open("scores.txt", "w")
file.write("Alice,10\n")
file.write("Bob,8\n")
file.close()
Write mode creates the file if it does not exist, and overwrites it if it does. Always close() the file when you are done.
Reading from a file
file = open("scores.txt", "r")
contents = file.read()
print(contents)
file.close()
Reading line by line
file = open("scores.txt", "r")
for line in file:
print(line.strip())
file.close()
strip() removes the invisible newline character at the end of each line.
Appending to a file
Append mode ("a") adds to the end of a file without deleting what is already there:
file = open("scores.txt", "a")
file.write("Carol,9\n")
file.close()
The three file modes to remember
"r"— read (the default)"w"— write (overwrites the file)"a"— append (adds to the end)
File handling is hard to practise on a locked-down school computer — but our free online Python IDE runs real Python that supports reading and writing files safely in your browser, so you can try every example above.
Try file handling in your browser.
Our Python IDE supports real file handling — give it a go.
Open the Python IDE →