Python Diary Application: Write and Save Daily Entries

 

 

Introduction:
A diary application is a useful tool for recording your daily thoughts, experiences, and reflections. In this guide, we will create a simple Python program that allows users to write daily entries and save them to a file. The application will provide a user-friendly interface where entries can be added, viewed, and stored for future reference.

Objective:
The goal of this project is to help you create a Python-based diary application. It will let users input their diary entries, which will be saved in a text file with each entry labeled by date. This program is a great way to practice file handling, user input, and basic Python functionality.

Python Code for Diary Application

# Python Diary Application

import os
from datetime import datetime

# Function to create and save a diary entry
def write_entry():
    # Get the current date
    current_date = datetime.now().strftime("%Y-%m-%d")
    
    # Ask the user to write their diary entry
    print(f"Writing entry for {current_date}")
    entry = input("Write your diary entry (press Enter when done): ")
    
    # Save the entry to a file with the date as the filename
    filename = f"diary_{current_date}.txt"
    with open(filename, "w") as file:
        file.write(f"Diary Entry for {current_date}\n\n")
        file.write(entry)

    print(f"Diary entry for {current_date} has been saved successfully!")

# Function to view existing entries
def view_entries():
    entries = [f for f in os.listdir() if f.startswith("diary_") and f.endswith(".txt")]
    
    if not entries:
        print("No diary entries found.")
    else:
        for entry in entries:
            print(f"\n--- {entry} ---")
            with open(entry, "r") as file:
                print(file.read())

# Main function to run the diary application
def diary_app():
    while True:
        print("\nDiary Application Menu:")
        print("1. Write a new entry")
        print("2. View past entries")
        print("3. Exit")
        
        choice = input("Choose an option: ")
        
        if choice == "1":
            write_entry()
        elif choice == "2":
            view_entries()
        elif choice == "3":
            print("Goodbye!")
            break
        else:
            print("Invalid option. Please try again.")

# Run the diary application
if __name__ == "__main__":
    diary_app()

Explanation of the Program Structure

This Python program is designed to allow users to write, view, and save their diary entries. Let’s break down the program structure:

  • Imports: We import the os module to handle file operations and the datetime module to get the current date.
  • write_entry() function: This function allows users to input their diary entries, which are saved to a text file with the current date as the filename.
  • view_entries() function: This function retrieves all the saved diary entries and displays them to the user.
  • diary_app() function: The main function which provides the user with a menu to either write a new entry, view past entries, or exit the program. This loop continues until the user chooses to exit.
  • File handling: The diary entries are saved to text files with a filename format of diary_YYYY-MM-DD.txt, allowing users to easily access specific entries by date.

How to Run the Program

Follow these steps to run the Python diary application:

  1. Make sure you have Python installed on your system (version 3.x recommended).
  2. Copy the code into a text editor (like Notepad or VS Code) and save it as diary_app.py.
  3. Open your command line or terminal and navigate to the folder where the diary_app.py file is saved.
  4. Run the program by typing python diary_app.py in the terminal and press Enter.
  5. Follow the on-screen menu to either write a new entry or view existing diary entries.
© 2025 Learn Programming. All Rights Reserved.

 

Leave a Reply

Your email address will not be published. Required fields are marked *