The Palos Publishing Company

Follow Us On The X Platform @PalosPublishing
Categories We Write About

Build a CLI blog manager

Building a CLI (Command Line Interface) blog manager is a great project that involves basic CRUD operations (Create, Read, Update, Delete) for blog posts, stored locally (e.g., as files or in a simple database). Below is a complete Python example that lets you manage blog posts from the terminal. It supports:

  • Creating new posts

  • Listing posts

  • Reading a post

  • Updating a post

  • Deleting a post

Each post is stored as a Markdown file (.md) in a folder named posts.


python
import os import sys from datetime import datetime POSTS_DIR = "posts" def init(): if not os.path.exists(POSTS_DIR): os.makedirs(POSTS_DIR) def list_posts(): posts = sorted(os.listdir(POSTS_DIR)) if not posts: print("No posts found.") return for i, post in enumerate(posts, 1): print(f"{i}. {post[:-3]}") # strip .md def create_post(): title = input("Enter post title: ").strip() if not title: print("Title cannot be empty.") return filename = title.lower().replace(" ", "_") + ".md" filepath = os.path.join(POSTS_DIR, filename) if os.path.exists(filepath): print("Post with this title already exists.") return print("Enter post content (end with a single '.' on a line):") lines = [] while True: line = input() if line.strip() == ".": break lines.append(line) content = "n".join(lines) with open(filepath, "w", encoding="utf-8") as f: f.write(f"# {title}nn") f.write(f"*Created: {datetime.now().strftime('%Y-%m-%d %H:%M:%S')}*nn") f.write(content) print(f"Post '{title}' created.") def read_post(): list_posts() choice = input("Enter post number to read: ").strip() try: idx = int(choice) - 1 posts = sorted(os.listdir(POSTS_DIR)) if idx < 0 or idx >= len(posts): print("Invalid post number.") return filepath = os.path.join(POSTS_DIR, posts[idx]) with open(filepath, "r", encoding="utf-8") as f: print("n" + f.read()) except ValueError: print("Please enter a valid number.") def update_post(): list_posts() choice = input("Enter post number to update: ").strip() try: idx = int(choice) - 1 posts = sorted(os.listdir(POSTS_DIR)) if idx < 0 or idx >= len(posts): print("Invalid post number.") return filepath = os.path.join(POSTS_DIR, posts[idx]) with open(filepath, "r", encoding="utf-8") as f: content = f.read().split("n", 4)[4:] # skip title, date header print("Current content:") print("n".join(content)) print("Enter new content (end with a single '.' on a line):") lines = [] while True: line = input() if line.strip() == ".": break lines.append(line) new_content = "n".join(lines) title = posts[idx][:-3].replace("_", " ").title() with open(filepath, "w", encoding="utf-8") as f: f.write(f"# {title}nn") f.write(f"*Updated: {datetime.now().strftime('%Y-%m-%d %H:%M:%S')}*nn") f.write(new_content) print(f"Post '{title}' updated.") except ValueError: print("Please enter a valid number.") def delete_post(): list_posts() choice = input("Enter post number to delete: ").strip() try: idx = int(choice) - 1 posts = sorted(os.listdir(POSTS_DIR)) if idx < 0 or idx >= len(posts): print("Invalid post number.") return filepath = os.path.join(POSTS_DIR, posts[idx]) os.remove(filepath) print("Post deleted.") except ValueError: print("Please enter a valid number.") def help_menu(): print(""" Commands: list - List all blog posts create - Create a new blog post read - Read a blog post update - Update a blog post delete - Delete a blog post help - Show this help menu exit - Exit the program """) def main(): init() print("Welcome to CLI Blog Manager!") help_menu() while True: cmd = input("nEnter command: ").strip().lower() if cmd == "list": list_posts() elif cmd == "create": create_post() elif cmd == "read": read_post() elif cmd == "update": update_post() elif cmd == "delete": delete_post() elif cmd == "help": help_menu() elif cmd == "exit": print("Goodbye!") break else: print("Unknown command. Type 'help' for options.") if __name__ == "__main__": main()

How to use:

  1. Save the above code to blog_manager.py.

  2. Run it with python blog_manager.py.

  3. Use the commands shown (list, create, read, update, delete, help, exit) to manage your posts.

This script is easily extendable—for example, adding tags, categories, or publishing functionality, or connecting to a database. Let me know if you want me to build an enhanced version!

Share this Page your favorite way: Click any app below to share.

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

We respect your email privacy

Categories We Write About