🐍 Python for kids · Class 5 · Lesson 17 of 18 · Free
Working with Files – Save and Read Data
⏱ About 25 minutes · ▶ run real Python here — nothing to install · no sign-in
In this lesson you will
- Write text into a file with open() and the "w" mode.
- Read a file back with read(), readlines() or a for loop.
- Add new lines to the end of a file with the "a" mode.
When you switch off a calculator, the numbers on its screen are gone. Python variables are the same: they vanish when the program ends. Files are like a notebook. What you write there stays, so you can read it again later.
Why do we save data in a file?
A variable lives in the computer's memory only while your program runs. When the program stops, the value is lost.
A file keeps the data even after the program ends. Your game's high score, a diary, or a class list can all be saved in files.
To use a file, you open it, work with it, and close it. It is just like a real notebook.
I saved a file called hello.txt
💡 Files are made in the current folder. On this web page, they live only in the page's memory and disappear when you close the tab.
What does the with statement do?
Forgetting close() can lose your data. The with statement closes the file for you, automatically.
with open(...) as f: opens the file and names it f. All the indented lines can use f. When the block ends, Python closes the file.
The "w" means write mode. It makes a new, empty file. If the file already exists, its old text is wiped out!
▶with open("diary.txt", "w") as f:2 f.write("Today we had a maths test.\n")3 f.write("I got 18 out of 20!\n")45with open("diary.txt", "r") as f:6 text = f.read()7print(text)
No variables yet.
▌✏️ Change this code and run it yourself
Today we had a maths test. I got 18 out of 20!
💡 write() does not add a new line by itself. Put \n at the end of each line.
How do I read a file in Python?
Open the file with "r", which means read mode. Then choose how to read it.
read() gives the whole file as one string. readlines() gives a list, with one string for each line.
Each line from readlines() still has \n at its end. Use .strip() to remove it before you print.
['mango\n', 'banana\n', 'guava\n'] Number of fruits: 3 First fruit: mango
How is the "a" mode different from "w"?
"a" means append mode. It adds new text at the end and keeps everything already in the file.
Use "w" to start a fresh file. Use "a" to add to it, like adding a new score after every match.
Below, we write one score with "w", then add two more with "a". Then a for loop reads the file line by line.
▶with open("scores.txt", "w") as f:2 f.write("Rohit 45\n")3with open("scores.txt", "a") as f:4 f.write("Virat 72\n")5 f.write("Shubman 30\n")67with open("scores.txt", "r") as f:8 for line in f:9 print(line.strip())
No variables yet.
▌✏️ Change this code and run it yourself
Rohit 45 Virat 72 Shubman 30
How do I save numbers in a file?
A file stores only text. So turn a number into a string with str() before you write it.
When you read it back, it is text again. Use int() to turn it into a number, so you can add it up.
Here, we save the marks of a class and then find the total from the file.
Total from the file: 255
💡 int() ignores the \n at the end of a line, so int("78\n") gives 78.
🤔 Predict the output
Think like Python: read the code and choose what it prints. You get the reason straight away.
Question 1. What does this print?
with open("a.txt", "w") as f:
f.write("Hi")
f.write("There")
with open("a.txt", "r") as f:
print(f.read())Question 2. What does this print?
with open("b.txt", "w") as f:
f.write("one\n")
with open("b.txt", "w") as f:
f.write("two\n")
with open("b.txt", "r") as f:
print(f.read().strip())Question 3. What does this print?
with open("c.txt", "w") as f:
f.write("red\n")
with open("c.txt", "a") as f:
f.write("blue\n")
with open("c.txt", "r") as f:
print(len(f.readlines()))🧩 Your challenge
Make a shopping list saver. Write three items into a file called shopping.txt, one per line. Then read the file and print each item with a number, like "1. rice".
1. rice 2. dal 3. milk
⚠️ Common mistakes (and how to fix them)
❌ Wrong
with open("homework_list.txt", "r") as f:
print(f.read())FileNotFoundError: [Errno 2] No such file or directory: 'homework_list.txt'
✅ Right
with open("homework_list.txt", "w") as f:
f.write("Maths page 12\n")
with open("homework_list.txt", "r") as f:
print(f.read())You can only read a file that already exists, so write it first (or check the file name).
❌ Wrong
with open("score.txt", "w") as f:
f.write(95)TypeError: write() argument must be str, not int
✅ Right
with open("score.txt", "w") as f:
f.write(str(95))Files store text only, so turn numbers into strings with str() first.
❌ Wrong
with open("team.txt", "w") as f:
f.write("Anika")
f.write("Ravi")
f.write("Zoya")No error — but the file holds AnikaRaviZoya on one line.
✅ Right
with open("team.txt", "w") as f:
f.write("Anika\n")
f.write("Ravi\n")
f.write("Zoya\n")write() never adds a new line by itself, so end each line with \n.
📌 Remember
- Files keep data after the program ends; variables do not.
- open(name, "w") makes a fresh file, "a" adds to the end, "r" reads it.
- with open(...) as f: closes the file for you when the block ends.
- write() needs a string and does not add \n by itself.
- read() gives the whole text; readlines() gives a list of lines; a for loop reads line by line.
- Use str() before writing numbers and int() after reading them.
Finished the quiz and the challenge?
Questions students ask
Where is my file saved?
Python saves it in the current folder, the folder your program runs from. On this web page, files live only in the page's memory. They disappear when you close or refresh the tab.
What is the difference between "w" and "a" mode?
"w" starts the file fresh and wipes any old text. "a" keeps the old text and adds the new text at the end.
Why should I use with open() instead of just open()?
with closes the file automatically, even if an error happens. With plain open(), you must remember to call close() yourself.
Why are there empty lines when I print lines from a file?
Each line already ends with \n, and print() adds another one. Use line.strip() to remove the extra \n before printing.