DEV Community

Cover image for Build a Simple CLI Task Manager in Python πŸš€!!
Nishkarsh Pandey
Nishkarsh Pandey

Posted on

3 2 2 2 2

Build a Simple CLI Task Manager in Python πŸš€!!

Managing your daily tasks right from the terminal is super handy. Today, I’ll show you how to build a simple Command Line Interface (CLI) Task Manager in Python β€” no fancy UI, just pure productivity!

Why Build a CLI Task Manager?
Quick to use from your terminal or SSH session.
Lightweight, no distractions.
Perfect for learning Python scripting and file handling.
Easily extendable as you grow your skills.

Features We’ll Build:

Add new tasks.
View all tasks.
Mark tasks as completed.
Delete tasks.
Save tasks persistently in a file (tasks.txt).

The Code:

import os
TASKS_FILE = "tasks.txt"
def load_tasks():
    if not os.path.exists(TASKS_FILE):
        return []
    with open(TASKS_FILE, "r") as f:
        tasks = [line.strip() for line in f.readlines()]
    return tasks

def save_tasks(tasks):
    with open(TASKS_FILE, "w") as f:
        for task in tasks:
            f.write(task + "\n")

def show_tasks(tasks):
    if not tasks:
        print("No tasks found. Time to chill! 😎")
    else:
        for idx, task in enumerate(tasks, 1):
            status = "[x]" if task.startswith("[x]") else "[ ]"
            task_text = task[3:] if task.startswith("[x]") or task.startswith("[ ]") else task
            print(f"{idx}. {status} {task_text}")

def add_task(tasks):
    task = input("Enter your new task: ").strip()
    if task:
        tasks.append("[ ] " + task)
        print(f'Added task: "{task}"')
    else:
        print("Task cannot be empty!")

def complete_task(tasks):
    show_tasks(tasks)
    try:
        num = int(input("Enter the task number to mark as complete: "))
        if 1 <= num <= len(tasks):
            tasks[num-1] = "[x] " + tasks[num-1][3:]
            print("Task marked as complete!")
        else:
            print("Invalid task number.")
    except ValueError:
        print("Please enter a valid number.")

def delete_task(tasks):
    show_tasks(tasks)
    try:
        num = int(input("Enter the task number to delete: "))
        if 1 <= num <= len(tasks):
            removed = tasks.pop(num-1)
            print(f'Removed task: "{removed[3:]}"')
        else:
            print("Invalid task number.")
    except ValueError:
        print("Please enter a valid number.")

def main():
    tasks = load_tasks()
    while True:
        print("\n--- CLI Task Manager ---")
        print("1. Show Tasks")
        print("2. Add Task")
        print("3. Complete Task")
        print("4. Delete Task")
        print("5. Exit")
        choice = input("Choose an option: ")

        if choice == "1":
            show_tasks(tasks)
        elif choice == "2":
            add_task(tasks)
            save_tasks(tasks)
        elif choice == "3":
            complete_task(tasks)
            save_tasks(tasks)
        elif choice == "4":
            delete_task(tasks)
            save_tasks(tasks)
        elif choice == "5":
            print("Bye! Stay productive! πŸ‘‹")
            break
        else:
            print("Invalid option, please try again.")

if __name__ == "__main__":
    main()

Enter fullscreen mode Exit fullscreen mode

Output:

Image description

How to Use
Save the script as task_manager.py
Run in terminal:

python task_manager.py
Enter fullscreen mode Exit fullscreen mode

Choose options by typing the number.
Your tasks will be saved in tasks.txt automatically.

What’s Next?

Add task deadlines and reminders.
Sort tasks by priority or date.
Sync tasks with Google Calendar or Todoist APIs.
Add colors using the termcolor or your own termstyle package!

Final Thoughts!!

Building a CLI Task Manager is a great beginner project that combines file handling, user input, and basic data management. Plus, it’s actually useful!

Feel free to fork and improve it on GitHub β€” and share your version in the comments!

AWS GenAI LIVE image

Real challenges. Real solutions. Real talk.

From technical discussions to philosophical debates, AWS and AWS Partners examine the impact and evolution of gen AI.

Learn more

Top comments (0)

ACI image

ACI.dev: Fully Open-source AI Agent Tool-Use Infra (Composio Alternative)

100% open-source tool-use platform (backend, dev portal, integration library, SDK/MCP) that connects your AI agents to 600+ tools with multi-tenant auth, granular permissions, and access through direct function calling or a unified MCP server.

Check out our GitHub!