Categories We Write About

Create a terminal-based journal app

Here’s a simple terminal-based journal app written in Python. It supports adding journal entries with timestamps, viewing all entries, and searching entries by keyword. The entries are saved in a local file (journal.txt).

python
import os from datetime import datetime JOURNAL_FILE = "journal.txt" def add_entry(): print("nWrite your journal entry. Type 'END' on a new line to finish.n") lines = [] while True: line = input() if line.strip().upper() == "END": break lines.append(line) entry_text = "n".join(lines).strip() if entry_text: timestamp = datetime.now().strftime("%Y-%m-%d %H:%M:%S") with open(JOURNAL_FILE, "a", encoding="utf-8") as f: f.write(f"{timestamp}n{entry_text}n---n") print("Entry added.n") else: print("Empty entry discarded.n") def view_entries(): if not os.path.exists(JOURNAL_FILE): print("nNo journal entries found.n") return print("nAll journal entries:n") with open(JOURNAL_FILE, "r", encoding="utf-8") as f: content = f.read().strip() if content == "": print("No journal entries found.n") return entries = content.split("---n") for i, entry in enumerate(entries, 1): print(f"Entry {i}:n{entry.strip()}n") def search_entries(): if not os.path.exists(JOURNAL_FILE): print("nNo journal entries found.n") return keyword = input("Enter keyword to search for: ").strip().lower() if not keyword: print("Empty keyword.n") return with open(JOURNAL_FILE, "r", encoding="utf-8") as f: content = f.read().strip() entries = content.split("---n") matched = [e.strip() for e in entries if keyword in e.lower()] if not matched: print(f"nNo entries found containing '{keyword}'.n") else: print(f"nEntries containing '{keyword}':n") for i, entry in enumerate(matched, 1): print(f"Match {i}:n{entry}n") def main(): while True: print("Journal App") print("1. Add entry") print("2. View all entries") print("3. Search entries") print("4. Exit") choice = input("Choose an option: ").strip() if choice == "1": add_entry() elif choice == "2": view_entries() elif choice == "3": search_entries() elif choice == "4": print("Goodbye!") break else: print("Invalid option. Try again.n") if __name__ == "__main__": main()

Run the script in your terminal. It will create a file journal.txt to store your entries.
Type your journal text line by line and finish by typing END on a new line.
Use the menu options to add, view, or search journal entries.

Share This Page:

Enter your email below to join The Palos Publishing Company Email List

We respect your email privacy

Comments

Leave a Reply

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

Categories We Write About