Skip to Content
Course content

105: Practice Exercise: Building a Simple File-Based Journal App

Click on the "Edit" button in the top corner of the screen to edit your slide content.

Alright, it's time to actually build something. We've talked about variables, loops, and file I/O, but putting them together in a real project is where the "aha!" moments usually happen. We're going to build a simple journal app. The goal is basic: the user writes a thought, and we save it to a file so it's still there when they restart the program.

The "Just Append It" Trap

If you were just hacking this together for a quick prototype, your first instinct might be to just open a text file in append mode and dump the user's string right into it. It looks something like this:

def save_entry(text):
    f = open("journal.txt", "a")
    f.write(text + "\n")
    f.close()

On the surface, this works. You run the script, you type "Today I learned about Python," and it's in the file. But as a professional, this is the kind of code that makes me nervous during a code review. Why? Because it's fragile. If the program crashes or the power cuts out right after open() but before close(), you risk corrupting the file. Plus, you're just dumping raw text into a void. If you want to go back and find an entry from last Tuesday, you're stuck reading the whole file and guessing where one entry ends and the next begins.

When the Data Becomes a Mess

The real problem with the naive approach is that it treats data as a stream of characters rather than a collection of objects. Imagine your journal grows to 1,000 entries. If you want to implement a feature to "Delete entry #42" or "Search for all entries containing 'Python'," you're going to have a nightmare of a time parsing a plain .txt file. You'd have to read the entire file into memory, split it by newlines, and hope the user didn't happen to press "Enter" inside their journal entry, which would break your entire parsing logic.

Structuring for the Future

The better way—and the way I'd want to see this implemented—is to use a context manager and a structured data format like JSON. Using with open(...) ensures the file closes itself even if an exception is raised, which saves you from those weird "file in use" errors on Windows. More importantly, by storing entries as a list of dictionaries, we can attach metadata—like a timestamp—to every single note.

Here is how I would approach the core logic:

import json
from datetime import datetime

def save_entry(text):
    # We load the existing entries first so we can append to the list
    try:
        with open("journal.json", "r") as f:
            entries = json.load(f)
    except (FileNotFoundError, json.JSONDecodeError):
        entries = []

    # Create a structured object for the entry
    new_entry = {
        "timestamp": datetime.now().strftime("%Y-%m-%d %H:%M:%S"),
        "content": text
    }
    entries.append(new_entry)

    # Write the whole list back to the file
    with open("journal.json", "w") as f:
        json.dump(entries, f, indent=4)

Now, we've traded a tiny bit of performance (since we're reading the whole file before writing) for an immense gain in reliability and flexibility. If we decide later that we want to add "Tags" or "Mood" to each journal entry, we just add another key to the dictionary. The structure stays intact, and our data remains clean.




📋 Practical Task

Project: Building a Timestamped Journal with JSON Persistence

Your task is to build a fully functional command-line Journal Application. Instead of just a single save function, you need to create a program that runs in a loop and gives the user a menu of options.

Requirements:

  • The Main Loop: The program should stay open until the user types 'quit'.
  • Adding Entries: Allow the user to input a journal entry. Save it to a file named my_journal.json using the structured approach (JSON) discussed in the lesson, including a timestamp for each entry.
  • Viewing Entries: Create an option that reads the JSON file and prints all past entries in a clean format (e.g., [2023-10-27 14:00] - This was a great day!).
  • Error Handling: Ensure the program doesn't crash if my_journal.json doesn't exist yet when the user tries to view their entries for the first time.
Rating
0 0

There are no comments for now.

to be the first to leave a comment.