# tracker.
txt
# Simple text-based habit / task tracker in Python.
# Tasks are stored in a file so they persist between runs.
import os
import datetime
DATA_FILE = "tracker_data.txt"
def load_tasks():
"""Load tasks from file."""
tasks = []
if [Link](DATA_FILE):
with open(DATA_FILE, "r", encoding="utf-8") as f:
for line in f:
line = [Link]()
if line:
[Link](line)
return tasks
def save_tasks(tasks):
"""Save tasks to file."""
with open(DATA_FILE, "w", encoding="utf-8") as f:
for task in tasks:
[Link](task + "\n")
def add_task(tasks):
"""Add a new task."""
task = input("Enter new task: ").strip()
if task:
now = [Link]().strftime("%Y-%m-%d %H:%M:%S")
[Link](f"[{now}] {task}")
print("Task added!")
def list_tasks(tasks):
"""List all tasks."""
if tasks:
print("\nYour tasks:")
for i, task in enumerate(tasks, 1):
print(f"{i:2d}. {task}")
else:
print("\nNo tasks yet.")
def mark_done(tasks):
"""Mark a task as done (just prints it as done)."""
if not tasks:
print("No tasks to mark.")
return
list_tasks(tasks)
try:
idx = int(input("Enter task number to mark as done: ")) - 1
if 0 <= idx < len(tasks):
task = tasks[idx]
now = [Link]().strftime("%Y-%m-%d %H:%M:%S")
tasks[idx] = f"[DONE {now}] {task}"
print("Task marked as done.")
else:
print("Invalid number.")
except ValueError:
print("Please enter a valid number.")
def delete_task(tasks):
"""Delete a task."""
if not tasks:
print("No tasks to delete.")
return
list_tasks(tasks)
try:
idx = int(input("Enter task number to delete: ")) - 1
if 0 <= idx < len(tasks):
deleted = [Link](idx)
print(f"Deleted: {deleted}")
else:
print("Invalid number.")
except ValueError:
print("Please enter a valid number.")
def main():
print("Simple Task Tracker")
print("Commands: add, list, done, delete, quit\n")
tasks = load_tasks()
while True:
cmd = input(">>> ").strip().lower()
if cmd in ("add", "a"):
add_task(tasks)
elif cmd in ("listdelete", "del"):
delete_task(tasks)
elif cmd in ("quit", "q", "exit"):
save_tasks(tasks)
print("Tracker closed. Tasks saved.")
break
else:
print("Use: add, list, done, delete, or quit.")
if __name__ == "__main__":
main()