๐Ÿ—ƒ๏ธ
๐Ÿ—ƒ๏ธ Working with Data ยท DATA-201 ยท Session 2 of 6

Storing and Loading Data

The practical heartbeat of a data app: save your records so they persist, and load them back when you start. Simple to do well, and the foundation everything else builds on.

โฑ 9 min read๐Ÿ“… Updated 2026-08-05๐Ÿ“Š Tables, charts & examples

The practical core of working with data is a simple, rhythmic pattern: load your data when the program starts, work with it in memory, and save it back whenever it changes. Do that well and your app reliably remembers everything. This session covers the everyday mechanics of persistence - the formats, the save/load cycle, and using a small store to make it effortless - the foundation the rest of data work builds on.

The save/load cycle

Persistence is a loop: read the data in at startup, keep it in memory while you use it, and write it out when it changes. Getting this right is most of the battle.

import json

def load(path):
    try:
        with open(path) as f:
            return json.load(f)
    except FileNotFoundError:
        return []            # first run: start empty

def save(path, data):
    with open(path, "w") as f:
        json.dump(data, f)
๐ŸŽฏ
Load at start, save on change, handle the first run. The pattern is always the same: when the app starts, load existing data (and if there's none yet - the first run - start with a sensible empty default rather than crashing); as the user does things, update the data in memory; and after any change, save it back to storage. Two common bugs to avoid: forgetting to save (so changes are lost) and not handling the missing-file first run (so the app crashes before any data exists). Get those right and persistence just works.

Formats and a store

You persist data in a format. JSON suits most structured data; CSV suits tables. And a small store packages the whole save/load cycle for you.

ToolBest for
JSON (larzjson)Structured data - objects, lists, nesting
CSV (larzcsv)Flat tables - rows and columns
A store (larzstore)Records that persist, with ids, automatically
A databaseWhen you outgrow files (next sessions)

For most small apps, JSON is the default - it maps directly onto Python's dicts and lists. A store like <a href='/stack/larzstore/'>larzstore</a> goes further: it keeps a collection of records, gives each an id, and handles the saving for you, so you get persistence without writing the load/save code yourself. Choose the lightest tool that fits your data.

Persistence that just works

  • The cycle: load at start, work in memory, save on change.
  • Handle the first run - no file yet means start with an empty default, not a crash.
  • JSON for structured data, CSV for tables - pick what fits.
  • A store (larzstore) packages the save/load cycle so persistence is effortless.

๐Ÿงฉ Do it with LarzOS

Turn this lesson into action with the exact free tools and AI workflows for the job:

๐ŸŽฏ Key takeaways

  • The core of a data app is a cycle: load data at start, work with it in memory, save it on change.
  • Handle the first run gracefully - no file yet means start with an empty default, not a crash.
  • Use JSON for structured data and CSV for flat tables; pick the lightest format that fits.
  • A store (larzstore) packages the save/load cycle - records with ids that persist automatically.

Frequently asked

How often should I save - after every single change, or at certain points?

For most small apps, saving after every meaningful change is the simplest and safest approach, and it's what tools like larzstore do by default (autosave) - but there are reasons and ways to be smarter as apps grow. Saving after each change means your persisted data is always up to date, so if the program stops unexpectedly (a crash, a power loss, the user closing it abruptly), you lose nothing or almost nothing - the data on disk reflects the latest state. This is the right default for small applications because it's simple to reason about (change something, it's saved) and it protects against data loss, which is usually more valuable than the minor cost of writing the file frequently when your data is modest in size. The main downside is performance: writing to disk after every tiny change can become slow if you're making many rapid changes or the data is large, because each save rewrites the file. So as apps grow, you might optimise: save less frequently but at safe points (after a batch of related changes, periodically on a timer, and importantly before the program exits), or only save when there are actually unsaved changes (tracking a 'dirty' flag). Some apps save on a schedule and on exit rather than on every keystroke, accepting that a crash might lose the last few seconds of changes in exchange for better performance - a reasonable trade-off for some cases but not others (you wouldn't want a banking app to be casual about saving). There's also a subtlety around safety: naively saving by overwriting the file means a crash mid-write could corrupt it, so more robust approaches write to a temporary file and then swap it in, ensuring you always have a complete good file - something to be aware of for important data, though beyond what small projects usually need. The practical guidance: for small apps and while learning, save after each change (or use a store that autosaves) - it's simple and safe, and the performance cost is negligible for modest data. As your data grows or you make many rapid changes, consider saving at sensible points instead (after batches, periodically, and always before exit) and only when something actually changed. And for important data, learn about safe-write techniques. Start with the simple always-save approach; optimise the frequency only when performance actually becomes a problem, which for most small projects it never does. The cardinal sin is forgetting to save at all (losing changes); saving too often is a minor, easily-fixed efficiency concern, so err toward saving.

Why use a store library instead of just writing my own save and load functions?

Writing your own save/load functions is completely fine and educational - and for a simple app it may be all you need - but a store library saves you from repeatedly writing the same boilerplate and from the subtle bugs that boilerplate tends to have, letting you focus on your actual app. When you persist data by hand, you write (and rewrite, in every project) the code to open a file, serialise your data, handle the file-not-found first run, save after changes, and manage ids if your records need them - and it's easy to get pieces slightly wrong (forgetting to handle the missing file, forgetting to save somewhere, mishandling id assignment, accidentally letting a returned record be mutated in a way that corrupts your in-memory data). A store library like larzstore packages this common pattern correctly, once: it keeps a collection of records, assigns each an id automatically, saves to a file after changes without you writing the save calls, handles the empty/first-run case, and returns copies so results can't accidentally corrupt the stored data. So instead of insert/find/update/delete plus file handling being code you write and debug, they're methods you call, and the fiddly correctness details are handled. That's genuine value: less code, fewer bugs, and a clean interface (insert this record, find the ones matching this, update that one) that reads clearly. It's the same reason you use any well-made library rather than reinventing it - the problem is common and solved, so you build on the solution. That said, understanding how to save and load data yourself is worth learning (which is why this session shows the raw pattern), because it demystifies what the store is doing and because sometimes you need custom behaviour a library doesn't provide. So the honest guidance: learn the save/load cycle yourself so you understand persistence (and can do it when you need something custom), but for the common case of 'I have a collection of records that should persist', reach for a store like larzstore to avoid rewriting boilerplate and to get the details right for free. Use the raw approach when learning or when you have unusual needs; use a store to be productive and correct for the standard case. And note that as your data grows beyond what files handle well, you'd move from a file-based store to a database - at which point database libraries package that persistence for you similarly. The theme throughout is: understand the fundamentals, then use good tools that handle the common, error-prone patterns so you can spend your effort on what makes your app unique rather than on re-solving persistence.

Session 2 of 6 complete

Next up: Querying Your Data

Continue to Session 3 โ†’

Related lessons

More in this course

Course home → · Browse all courses →