Skip to content

Module 3: The Task Manager

Concept: File I/O, Sets, and Data Persistence.

Variables die when the program closes. We fix that by saving data to a text file.

📺 The Build Session

🧠 Key Concepts

  • Context Manager: with open(...) automatically closes files to prevent leaks.
  • Sets: A collection that automatically deletes duplicates.
  • Persistence: Writing to disk so data survives a reboot.

💻 The Code Blueprint

# 1. Load Data
try:
    with open("tasks.txt", "r") as file:
        tasks = [line.strip() for line in file.readlines()]
except FileNotFoundError:
    tasks = []

# 2. Add Task & Remove Duplicates
new_task = input("Add task: ")
tasks.append(new_task)
tasks = list(set(tasks))  # Remove duplicates

# 3. Save Data
with open("tasks.txt", "w") as file:
    for task in tasks:
        file.write(task + "\n")