Junior Codes
The Sky Expedition · stretch

Reading and Writing Files in Python for Kids

Save information to a file and read it back later.

Start Wonderwild free with Pip

Try 5 starter activities free. Full access is ₹499 a year.

This mission is part of full access. The first missions are free to try.

Files & with, explained simply

Variables are forgotten when a program ends. Files let a program remember things between runs, like a diary, a high score or a saved game.

open() takes a file name and a mode: "w" to write (replacing what was there), "a" to add to the end, and "r" to read.

Writing with open(...) as f: means Python closes the file for you when the indented block ends, even if something goes wrong. That is the safe, modern way to work with files.

Example: A journal that remembers

with open("journal.txt", "a") as f:
    f.write("Saw a red kite today\n")

with open("journal.txt", "r") as f:
    print(f.read())

Output

Saw a red kite today

\n adds a new line so each entry sits on its own line. Run it twice and the journal grows.

Try this

  • Change "a" to "w", run it twice, and notice the difference.
  • Save a high score to a file and read it back at the start of a game.
  • Loop over a file with for line in f: to handle one line at a time.

Practise it in Wonderwild

“A journal that remembers”

Mission 32 in The Sky Expedition, one of 36 Python missions in Wonderwild.

Your child writes and runs real Python in the browser while Pip reacts to every line. Nothing to install. For ages 7+.

Start Wonderwild free with Pip

Try 5 starter activities free. Full access is ₹499 a year.

This mission is part of full access. The first missions are free to try.

Questions parents and kids ask

Where does the file get saved?+

In the same folder the program runs from, unless you give a full path. Online editors each keep their own private files area.

Why use with instead of just open()?+

with closes the file automatically. Forgetting to close a file can mean your writing never gets saved.

Prefer learning with a teacher?

Our live Python for Young Developers course teaches these ideas in small online classes led by a software engineer, with projects and feedback. Every live course also includes 12 months of full Wonderwild access for practice.

See the Python for Young Developers course