如何修改外部文本文件任务的Yes/No状态及完善任务管理功能
问题:修改任务文件中的完成状态(毕业设计需求)
我需要把外部文本文件特定行里的"No"改成"Yes",但不想覆盖整个文件。这是我毕业设计(Capstone project)的一部分。
功能需求
当用户选择‘vm’查看分配给自己的所有任务时,需要实现以下功能:
- 以易读格式显示所有任务,每个任务带唯一识别编号
- 允许用户输入任务编号选择特定任务,或输入‘-1’返回主菜单
- 选中任务后,提供两种操作选项:
- 标记完成:将任务的完成状态(Yes/No)改为"Yes"
- 编辑任务:仅能修改未完成任务的负责人用户名或截止日期
现有代码
def view_mine(): f = open("tasks.txt", "r").readlines() for index, value in enumerate(f, 1): v = value.split(", ") print(f"Task Number: {index}") print(f"Task : {v[1]} Assigned to : {v[0]} Date Assigned : {v[3]} Due Date : {v[4]} Task Complete? {v[5]} Task Description : {v[2]} ") select_task = int(input("Please enter the Task Number of the Task you would like to edit:")) if select_task in range(1, select_task + 1): choice = input('''Please enter an edit option: c - Complete e - Edit 0 - Exit :''').lower() if choice == "0": return elif choice == "c": f = open("tasks.txt", "r").readlines() for line in f: if "No" in line: oriline = line newline = line.replace("No","Yes") elif "Yes" in line: print("This task is completed and cannot be edited.") f = open("tasks.txt", "r").read() filedata = f.replace(oriline, newline) f = open("tasks.txt", "w") f.write(filedata) print("The Task has been marked to completed.")
解决方案建议
核心问题修复
你的现有代码存在两个关键问题:
- 任务选择逻辑错误:
if select_task in range(1, select_task + 1)永远为真,无法正确判断输入的编号是否有效 - 修改方式错误:直接替换文件中所有"No"的行,会误改其他任务,且没有针对用户选中的任务操作
优化后的实现思路
- 仅显示当前用户的任务:先筛选出分配给当前用户的任务,避免显示无关内容
- 安全修改指定行:读取所有任务行到列表,修改选中的对应行后,再整体写回文件,既不会覆盖整个文件,也能精准修改目标任务
- 增加输入验证:处理无效输入,提升交互稳定性
- 完善操作逻辑:区分已完成/未完成任务的操作权限
修改后的代码示例
def view_mine(): # 读取所有任务,使用with自动管理文件关闭 with open("tasks.txt", "r") as f: all_tasks = f.readlines() # 替换为实际的当前登录用户名(比如从登录逻辑中获取) current_user = "current_logged_in_user" # 筛选当前用户的任务 user_tasks = [] for task_line in all_tasks: task_data = task_line.strip().split(", ") if task_data[0] == current_user: user_tasks.append(task_line) if not user_tasks: print("你没有分配到任何任务。") return # 带编号显示用户任务 for idx, task_line in enumerate(user_tasks, 1): task_data = task_line.strip().split(", ") print(f"任务编号: {idx}") print(f"任务名称: {task_data[1]}") print(f"负责人: {task_data[0]}") print(f"分配日期: {task_data[3]}") print(f"截止日期: {task_data[4]}") print(f"是否完成: {task_data[5]}") print(f"任务描述: {task_data[2]}\n") # 处理任务选择,支持-1返回 while True: user_input = input("请输入要操作的任务编号,或输入-1返回主菜单: ") if user_input == "-1": return try: selected_idx = int(user_input) if 1 <= selected_idx <= len(user_tasks): break print(f"请输入1到{len(user_tasks)}之间的有效编号!") except ValueError: print("输入无效,请输入数字!") # 找到选中任务在原列表中的位置 selected_task_line = user_tasks[selected_idx - 1] original_task_index = all_tasks.index(selected_task_line) task_data = selected_task_line.strip().split(", ") # 操作选择 action_choice = input('''请选择操作: c - 标记为完成 e - 编辑任务 0 - 返回 : ''').lower() if action_choice == "0": return elif action_choice == "c": if task_data[5] == "Yes": print("该任务已经完成,无需重复标记!") return # 修改完成状态 task_data[5] = "Yes" updated_line = ", ".join(task_data) + "\n" all_tasks[original_task_index] = updated_line # 写回文件 with open("tasks.txt", "w") as f: f.writelines(all_tasks) print("任务已成功标记为完成。") elif action_choice == "e": if task_data[5] == "Yes": print("已完成的任务无法编辑!") return # 编辑选项 edit_choice = input('''请选择要编辑的内容: u - 修改负责人用户名 d - 修改截止日期 0 - 返回 : ''').lower() if edit_choice == "0": return elif edit_choice == "u": new_user = input("请输入新的负责人用户名: ").strip() task_data[0] = new_user elif edit_choice == "d": new_due_date = input("请输入新的截止日期(和原格式保持一致): ").strip() task_data[4] = new_due_date else: print("无效选项,返回主菜单。") return # 更新任务行并写回 updated_line = ", ".join(task_data) + "\n" all_tasks[original_task_index] = updated_line with open("tasks.txt", "w") as f: f.writelines(all_tasks) print("任务已编辑完成。") else: print("无效选项,返回主菜单。")
关键说明
- 使用
with语句操作文件,避免手动关闭文件的遗漏和资源泄漏 - 仅修改选中的任务行,其他任务内容保持不变,解决"覆盖整个文件"的顾虑
- 增加了输入合法性校验,防止用户输入非数字或超出范围的编号
- 严格区分已完成和未完成任务的操作权限,符合需求要求
内容的提问来源于stack exchange,提问作者Hanri Swanepoel
相关产品推荐
相关产品推荐

