Rullst AcademyRullst Academy/Lesson
Python: from first line to project · Lesson 10 / 12

Files that remember after closing

Program memory ends when it closes. A file lets us find data again, provided writing and reading are checked.

Learning paths12 min

Let's discover together

  1. 1

    JSON is a text data format. Python uses False; JSON uses false. json.dump and json.load translate supported values such as lists, dictionaries, numbers and booleans.

  2. 2

    In version 1, Path points to my-missions.json in the terminal’s current folder. open("x") creates a new file and fails if it already exists. with closes the file when leaving the block.

  3. 3

    The second block opens the file for UTF-8 reading; json.load recovers the data. Run in a practice folder. To repeat, choose another name or preserve the earlier file; do not switch to "w" without understanding it replaces content.

  4. 4

    Valid JSON does not guarantee suitable data: "minutes": "ten" is still text. The final project checks fields and types. A saved file is not automatically a backup or a GitHub upload.

✦
YOUR MISSION

Write and reread JSON data; distinguish persistence, validation and backup.

Now it's your turn

Discovery workshop

Read, predict, check

Compare the two versions. Show result reveals a prepared output: this panel does not run Python. Then practise with the activity file.
Mini experiment

Learning simulation: compare cards and check your predictions. The browser is not compiling Rust or starting a server.

import json
from pathlib import Path

tasks = [{"title": "Tico", "minutes": 10, "done": False}]
path = Path("my-missions.json")
with path.open("x", encoding="utf-8") as file:
    json.dump(tasks, file)
with path.open(encoding="utf-8") as file:
    restored = json.load(file)
print(restored[0]["minutes"])
Build your answers and check when you are ready.
A moment to understand

What does open("x") do when the file already exists?

I need a hint

Mode x is exclusive creation, not overwriting.

Loading your journey…

Take it with you

Run in the practice folder and open my-missions.json in the editor. Run again and observe FileExistsError. Check that the first file was preserved. Then change the output name and a duration and repeat.

PYTHON · .py

Your Python workbench

Here you read and predict prepared outputs. To run the actual code, download the activity file, edit it on your computer and use Python. Rullst Academy grades lesson answers; it does not verify local execution.

Save and run

Extract the activity ZIP into its own folder. Open a terminal there and use your system’s command. If downloading your edited file separately, use its actual name in the command. Save before running again.

Windows
python lesson.py
macOS / Ubuntu
python3 lesson.py
My files and GitHub

The editor keeps a local draft and can send a .py file to GitHub with your confirmation. Download your edited code to preserve it. The ZIP is starter material; for the final project, also publish data and test files following PUBLISH.md.

At your own pace

My files and GitHub

Every lesson can become your own record: code, notes or a practice plan. Saving a file to GitHub is optional and does not change your XP.

Student guide

The editor and downloads open when this lesson is available to your account. Check access below or return to the track map.

Checking your access to this lesson…

Rullstpetbot

AI can make mistakes. Check the lesson material.