我刚刚开始学习Python,但在尝试运行一个程序(我自己编写的代码)时遇到了困难。命令提示符能够识别我安装的是Python 3.12版本,然而当我在VS Code中尝试运行我的代码文件时,这个文件并没有任何反应。我已经修正了目录路径问题,但问题依旧存在,仿佛系统并不能识别这个Python文件一样。
为了能够顺利运行我的程序,我编写了以下代码:
import os
from datetime import datetime, timedelta
def read_task(file1):
tasks = {}
if os.path.exists(file1):
with open(file, 'r')as file:
for line in file:
task, completed, creation_date = line.strip().split(',')
tasks[task] = {'completed': bool(int(completed)), 'creation_date': datetime.strptime(creation_date, '%Y-%m-%d')}
return task
def write_tasks(file1, tasks):
with open(file1, 'w') as file:
for task, details in task.items():
completed = details['completed']
creation_date = details[creation_date].strftime('%Y-%m-%d')
file.write (f'{task},{int(completed)},{creation_date}\n')
def main():
file1 = 'task.txt'
tasks = read_task(file1)
current_date = datetime.now()
for task, details in tasks.items():
completed = details['completed']
creation_date = details['creation_date']
if completed and (current_date - creation_date).days >= 7:
tasks[task][completed]= False
print ("Current tasks:")
completed = details['completed']
print (f'{task}: {"Done" if completed else "To do"}')
completed_task = input("Enter a completed task: ")
tasks[completed_task]['completed'] = True
write_tasks(file1, tasks)
if __name__ == "__main__":
main()