SoFunction
Updated on 2025-03-04

Building a simple task manager using Python

introduction

In modern software development, task management is a crucial feature. Whether it is personal projects or team collaboration, good task management can significantly improve work efficiency. With the continuous advancement of technology, many complex task management tools have emerged, but for beginners, understanding the basic task management logic and implementation methods are equally important.

This blog post aims to help readers master the basic concepts and practical skills of Python programming by building a simple task manager. We will start from scratch and gradually implement a fully functional command line task manager, covering basic operations such as adding, viewing, completing and deleting tasks. Through this project, readers can not only learn object-oriented programming in Python, but also master the basic skills of file operations and user interaction.

Whether you are a newbie in Python or a developer who wants to consolidate your programming foundation, this blog post will provide you with a clear learning path. Let's start this fun programming journey together!

Project Overview

In today's fast-paced life, task management tools have become a must-have tool for individuals and teams to work efficiently. Whether it’s a student, a freelancer or a corporate team, being able to effectively manage tasks and time is the key to success. To help users better organize and track their tasks, we will build a simple task manager application.

1. Project objectives

Our Task Manager is designed to provide a user-friendly interface that allows users to easily do the following:

Add a task: The user can enter a task description and the system saves it as a to-do item.

View all tasks: Users can view the current list of all tasks, including the completion status of the tasks.

Mark tasks as completed: Users can mark completed tasks as completed to facilitate tracking progress.

Delete tasks: Users can delete tasks that they no longer need to keep the task list neat.

2. Functional details

Task Description: Each task contains a description field, and users can customize task content according to their needs.

Completed Status: Each task has a Boolean value indicating whether the task has been completed. Users can update this state through command line interaction.

Persistent storage: Task data will be saved in a text file (), ensuring that task information can still be saved and loaded even after the program is closed.

Command line interface: The user will interact with the program through a simple command line interface, select different operations, enter task information, etc.

3. Technology stack

This project will use the following techniques and tools:

Programming Language: Python

Data storage: text file ()

User interface: Command line interface

4. Expected results

By completing this project, users will be able to:

Understand the concepts of Python's basic syntax and object-oriented programming.

Learn how to read and write files to achieve persistent storage of data.

Master basic command line interaction skills and be able to design a simple user interface.

Have the ability to independently develop small applications, laying the foundation for subsequent learning of more complex projects.

5. Project expansion

After implementing the basic functions, users can consider expanding the project, for example:

Add deadline: Add deadlines for each task so that users can better manage time.

Priority Settings: Allows users to prioritize tasks, helping them arrange their work more effectively.

Graphical User Interface: Extend the command line interface to a graphical user interface (GUI), using libraries such as Tkinter or PyQt.

Network synchronization: implements the cloud synchronization function of tasks, allowing users to access and manage tasks on different devices.

Through these extensions, users can further improve the complexity and practicality of the project and enhance their programming capabilities.

Project structure

A reasonable project structure is key to ensuring code maintainability and scalability when building a simple task manager application. We divide the project into several main sections, each with its specific functions and responsibilities. The following is a detailed structure description of the project:

task_manager/

├── task_manager.py
└──

1. task_manager.py

This is the main program file of the project, which contains the core logic and functional implementation of the task manager. This document will be responsible for the following aspects:

Task class definition: In this file, we define the Task class, which is used to represent properties and methods of a single task. Each task object will contain the task description and completion status and provide a method to mark completion.

Task Manager class definition: The TaskManager class will be responsible for managing the life cycle of all tasks, including loading, saving, adding, viewing, marking completion and deleting tasks. By encapsulating these functions in a class, we can better organize the code to make it easier to understand and maintain.

User interaction logic: In the main() function, we implement a simple command line interface that allows users to select different operations through input. This section will process user input and call the corresponding task manager method.

2.

This is a text file that persists the storage of task data. The structure of this file is very simple, each line represents a task, and the format is as follows:

Task Description | Complete Status

Task description: Represents the specific content of the task, such as "Complete Report" or "Attend to a Meeting".

Completed status: Use 0 to indicate incomplete, and use 1 to indicate completion. In this way, we can easily store the status of tasks in the file and load this information when the program starts.

3. Project expansion structure

Based on the basic structure, users can expand projects as needed. Here are some examples of possible extension structures:

task_manager/

├── task_manager.py
├──
├──
├──
└── utils/
    ├── date_utils.py
    └── priority_utils.py

: Contains the project description document, introducing the project's functions, installation steps and usage methods. This is very important for other developers or users to understand the project.

: List the third-party libraries and dependencies required by the project, making it easier for users to install and configure the environment.

utils/: A directory for storing accessibility functions. For example:

date_utils.py: Can contain functions that process dates and times to help users set deadlines for tasks.

priority_utils.py: Can contain functions that handle task priority, allowing users to assign priority to tasks.

4. Code organization principles

When designing a project structure, we follow the following code organization principles:

Modularity: Separate different functions for easy management and maintenance. Each class and function should have clear responsibilities.

Scalability: The project structure should support future expansions, allowing new features to be added without affecting existing code.

Readability: The code should be easy to understand, using clear naming and annotations to help other developers get started quickly.

Code implementation

1. Create a task class

We first create a Task class to represent the task object.

class Task:
    def __init__(self, description):
         = description
         = False

    def mark_completed(self):
         = True

    def __str__(self):
        status = "✓" if  else "✗"
        return f"[{status}] {}"

2. Task Manager Class

Next, we create a TaskManager class that manages the addition, viewing, completion, and deletion of tasks.

class TaskManager:
    def __init__(self, filename=''):
         = []
         = filename
        self.load_tasks()

    def load_tasks(self):
        try:
            with open(, 'r') as file:
                for line in file:
                    description, completed = ().split('|')
                    task = Task(description)
                    if completed == '1':
                        task.mark_completed()
                    (task)
        except FileNotFoundError:
            pass

    def save_tasks(self):
        with open(, 'w') as file:
            for task in :
                completed = '1' if  else '0'
                (f"{}|{completed}\n")

    def add_task(self, description):
        task = Task(description)
        (task)
        self.save_tasks()

    def view_tasks(self):
        for index, task in enumerate():
            print(f"{index + 1}. {task}")

    def mark_task_completed(self, index):
        if 0 <= index < len():
            [index].mark_completed()
            self.save_tasks()

    def delete_task(self, index):
        if 0 <= index < len():
            del [index]
            self.save_tasks()

3. User interaction

Finally, we implement a simple command line interface that allows users to interact with the task manager.

def main():
    manager = TaskManager()

    while True:
        print("\nTask Manager")
        print("1. Add a task")
        print("2. View tasks")
        print("3. Mark the task as completed")
        print("4. Delete Task")
        print("5. Exit")

        choice = input("Please select an action: ")

        if choice == '1':
            description = input("Input task description: ")
            manager.add_task(description)
        elif choice == '2':
            manager.view_tasks()
        elif choice == '3':
            index = int(input("Enter the task number: ")) - 1
            manager.mark_task_completed(index)
        elif choice == '4':
            index = int(input("Enter the task number: ")) - 1
            manager.delete_task(index)
        elif choice == '5':
            break
        else:
            print("Invalid selection, please try again.")

if __name__ == "__main__":
    main()

Run the project

To run this project, just execute the following command on the command line:

python task_manager.py

Summarize

By building this simple task manager, we not only implement a practical tool, but also have a deep understanding of the basic concepts and practical skills of Python programming. In the project, we learned how to organize code using object-oriented programming, how to perform file operations to enable persistent storage of data, and how to design a simple command line interface to interact with users.

The success of this project is not only due to its functionality implementation, but also because it provides us with a good learning platform, allowing us to consolidate and expand our programming knowledge in practical applications. In the future, we can make more extensions based on this, such as adding a graphical user interface, implementing cloud synchronization of tasks, or introducing more complex task management functions.

The above is the detailed content of using Python to build a simple task manager. For more information about building a task manager in Python, please follow my other related articles!