Simple Note Keeper Project (code) in Python

← Back to Projects

Simple Note Keeper in Python.

About the project: This program will let you add new notes, view all your saved notes, and delete notes you no longer need.

All your notes will be saved to a file, so they will be there even after you close and reopen the app.

It provides a menu-driven interface for managing your notes, and all your changes are saved to a notes.json file, ensuring your notes are persistent between sessions.

How to use this program:

  • Run the Python script.
  • It will guide you through adding, viewing, and deleting notes.

Project Level: Beginner

You can directly copy the below snippet code with the help of green copy button, paste it and run it in any Python editor you have.

Steps: Follow these steps

Step 1: Copy below code using green 'copy' button.

Step 2: Paste the code on your chosen editor.

Step 3: Save the code with filename and .py extention.

Step 4: Run (Press F5 if using python IDLE)




# simple_note_keeper.py

import json
import os
from datetime import datetime

# Filename for storing notes
NOTES_FILE = "notes.json"

def load_notes():
    """
    Loads notes from a JSON file. If the file doesn't exist or is empty/corrupted,
    it returns an empty dictionary.
    """
    if os.path.exists(NOTES_FILE) and os.stat(NOTES_FILE).st_size > 0:
        try:
            with open(NOTES_FILE, 'r') as f:
                return json.load(f)
        except json.JSONDecodeError:
            print("Warning: Notes file is corrupted or empty. Starting with a new note book.")
            return {}
    return {}

def save_notes(notes):
    """
    Saves the notes dictionary to a JSON file.
    """
    with open(NOTES_FILE, 'w') as f:
        json.dump(notes, f, indent=4)
    print("Notes saved successfully.")

def add_note(notes):
    """
    Adds a new note with a title and content.
    The title will be used as the key for the note.
    """
    title = input("Enter note title: ").strip()
    if not title:
        print("Note title cannot be empty. Note not added.")
        return

    if title in notes:
        print(f"A note with the title '{title}' already exists. Please choose a different title.")
        return

    content = input("Enter note content: ").strip()
    timestamp = datetime.now().strftime("%Y-%m-%d %H:%M:%S")

    notes[title] = {
        "content": content,
        "timestamp": timestamp
    }
    print(f"Note '{title}' added.")

def view_notes(notes):
    """
    Displays all saved notes, sorted by title.
    """
    if not notes:
        print("\nNo notes found.")
        return

    print("\n--- Your Notes ---")
    # Sort notes by title for consistent display
    sorted_titles = sorted(notes.keys())
    for title in sorted_titles:
        details = notes[title]
        print(f"\nTitle: {title}")
        print(f"  Created/Last Updated: {details.get('timestamp', 'N/A')}")
        print(f"  Content: {details.get('content', 'N/A')}")
    print("------------------")

def delete_note(notes):
    """
    Deletes a note by its title.
    """
    title = input("Enter the title of the note to delete: ").strip()
    if not title:
        print("Title cannot be empty.")
        return

    if title in notes:
        del notes[title]
        print(f"Note '{title}' deleted.")
    else:
        print(f"Note '{title}' not found.")

def main():
    """
    Main function to run the Simple Note Keeper app with a menu.
    """
    notes = load_notes()

    print("--- Python Simple Note Keeper ---")

    while True:
        print("\nMenu:")
        print("1. Add a new note")
        print("2. View all notes")
        print("3. Delete a note")
        print("4. Quit and Save")

        choice = input("Enter your choice (1-4): ").strip()

        if choice == '1':
            add_note(notes)
        elif choice == '2':
            view_notes(notes)
        elif choice == '3':
            delete_note(notes)
        elif choice == '4':
            save_notes(notes)
            print("Exiting Simple Note Keeper. Goodbye!")
            break
        else:
            print("Invalid choice. Please enter a number between 1 and 4.")

# This ensures that main() is called only when the script is executed directly.
if __name__ == "__main__":
    main()