Make a simple task manager using python

This article teaches you how to create a simple task manager application using Python. This project will help you practice many aspects of Python programming, including file manipulation, user input handling, and basic command line interface design. In this article, I'll guide you through creating a basic command line task manager.

Insert image description here

Task manager purpose

Task manager is a tool widely used in daily life and work. It can help you:

  • Increase productivity : By logging and tracking tasks, you can better organize your time and ensure important tasks don't get overlooked.

  • Avoid omissions : No more relying on memory to record tasks. Task Manager ensures your to-do list is always available.

  • Collaborate and share : Some task manager apps allow you to collaborate with others and share task lists, which is especially useful in team projects.

  • Analyze and optimize : By viewing completed tasks, you can analyze your work habits and find out which tasks need more attention.

Introduction to the Task Manager Project

Task Manager is a tool for creating, viewing, and deleting tasks. We use Python to build a simple command line task manager that can do the following:

  1. Add task
  2. View task list
  3. Delete task

Required skills and tools

Before you begin, make sure you have Python installed and have the following knowledge and tools:

  1. Basic Python programming knowledge.
  2. Experience using command line interface (terminal).

Project steps

Step 1: Initialize the task list

First, we need to initialize a task list. We will use a text file to save the tasks, one line per task. Create a tasks.txtfile named to save the task.

# 初始化任务列表
with open("tasks.txt", "w") as file:
    pass  # 创建一个空文件

Step 2: Add tasks

Enable users to add tasks. We will write a function that allows the user to enter a description of the task and then add the task to the task list.

def add_task():
    task_description = input("请输入任务的描述:")

    # 打开任务列表文件并追加任务
    with open("tasks.txt", "a") as file:
        file.write(task_description + "\n")

    print("任务已成功添加!")

# 调用添加任务函数
add_task()

Step 3: Review the task list

The user should be able to view the task list. We will write a function that will open the task list file and display all tasks.

def view_tasks():
    try:
        # 打开任务列表文件并读取任务
        with open("tasks.txt", "r") as file:
            tasks = file.readlines()

        if tasks:
            print("任务列表:")
            for i, task in enumerate(tasks, start=1):
                print(f"{
      
      i}. {
      
      task.strip()}")
        else:
            print("任务列表为空。")
    except FileNotFoundError:
        print("任务列表文件不存在。")

# 调用查看任务列表函数
view_tasks()

Step 4: Delete tasks

Finally, the user should be able to delete the task. We will write a function that allows the user to enter the serial number of the task to be deleted and delete the corresponding task from the task list.

def delete_task():
    try:
        # 打开任务列表文件并读取任务
        with open("tasks.txt", "r") as file:
            tasks = file.readlines()

        if tasks:
            view_tasks()  # 显示任务列表以供选择
            task_number = int(input("请输入要删除的任务的序号:"))

            if 1 <= task_number <= len(tasks):
                # 删除选定的任务
                del tasks[task_number - 1]

                # 写入更新后的任务列表
                with open("tasks.txt", "w") as file:
                    file.writelines(tasks)

                print("任务已成功删除。")
            else:
                print("无效的任务序号。")
        else:
            print("任务列表为空。")
    except FileNotFoundError:
        print("任务列表文件不存在。")

# 调用删除任务函数
delete_task()

Now, let's move on to improving our task manager project.

Step 5: Mark completed tasks

To better track the status of tasks, we can add a feature that allows users to mark tasks as completed.

def mark_task_complete():
    try:
        # 打开任务列表文件并读取任务
        with open("tasks.txt", "r") as file:
            tasks = file.readlines()

        if tasks:
            view_tasks()  # 显示任务列表以供选择
            task_number = int(input("请输入要标记为已完成的任务的序号:"))

            if 1 <= task_number <= len(tasks):
                # 标记选定的任务为已完成
                tasks[task_number - 1] = "[已完成] " + tasks[task_number - 1]

                # 写入更新后的任务列表
                with open("tasks.txt", "w") as file:
                    file.writelines(tasks)

                print("任务已成功标记为已完成。")
            else:
                print("无效的任务序号。")
        else:
            print("任务列表为空。")
    except FileNotFoundError:
        print("任务列表文件不存在。")

# 调用标记任务为已完成的函数
mark_task_complete()

Step 6: Set task deadlines

To plan tasks better, we can add a due date to each task. Let's update the function that adds the task so that it accepts a due date.

from datetime import datetime

def add_task_with_deadline():
    task_description = input("请输入任务的描述:")
    deadline_str = input("请输入任务的截止日期 (YYYY-MM-DD):")

    try:
        # 将截止日期字符串转换为日期对象
        deadline = datetime.strptime(deadline_str, "%Y-%m-%d")

        # 打开任务列表文件并追加任务(包括截止日期)
        with open("tasks.txt", "a") as file:
            file.write(f"{
      
      task_description} (截止日期:{
      
      deadline_str})\n")

        print("任务已成功添加!")
    except ValueError:
        print("无效的日期格式。请使用 YYYY-MM-DD 格式。")

# 调用添加任务函数(包括截止日期)
add_task_with_deadline()

By adding deadlines, you can better plan and schedule tasks, ensuring important tasks don't get delayed.

Future improvements to the project

Although we've created a basic task manager, there's a lot that can be improved and expanded upon. Here are some possible improvements and extension points:

  • Provide task priority : Add priority to tasks to help users identify which tasks are more important.

  • Provide search capabilities : Allow users to search for tasks, especially when the task list grows large.

  • Export and Import functionality : Allows users to export task lists to files or import tasks from files to back up or migrate tasks.

  • User authentication : If you plan to share your task list with others, you can add user authentication and permission controls.

This task manager project is a great starting point to inspire you to build more complex and feature-rich task management applications. Hope you enjoy this project and can take it to the next level!

Summarize

This simple task manager project can help you improve your Python programming skills, including file manipulation, user input processing, and basic command line interface design. You can extend this project as needed to add more features, such as marking completed tasks, setting task deadlines, etc. Task Manager is a very useful tool that can help you better organize and track tasks, whether at work or in your personal life. I hope this project is an interesting learning opportunity for you and inspires you to build more complex Python applications.

Guess you like

Origin blog.csdn.net/qq_44273429/article/details/132866395