Content · Programming · Python

Python Build a Command-Line Todo App

A command-line to-do application can store tasks in JSON and expose add and list commands through argparse.

2 views Published Oct 17, 2026 Updated Oct 18, 2026 Admin

A command-line to-do application can store tasks in JSON and expose add and list commands through argparse.

What It Does and Why It Matters


A command-line to-do application can store tasks in JSON and expose add and list commands through argparse. Use explicit encoding and controlled paths; for concurrent updates, add locking or a database.

Step-by-Step Example


Start with the complete example below. Read the inputs and predict what the final print statement will show before you run it.

import json
from pathlib import Path
import tempfile
with tempfile.TemporaryDirectory() as temporary_directory:
    task_path = Path(temporary_directory) / 'tasks.json'
    tasks = [{'task': 'review documentation', 'done': False}]
    task_path.write_text(json.dumps(tasks, indent=2), encoding='utf-8')
    loaded_tasks = json.loads(task_path.read_text(encoding='utf-8'))
    for task in loaded_tasks:
        print(task['task'])

How the Code Works


  • The imports make these standard-library tools available: json, pathlib, tempfile.
  • The loop advances through values one at a time. Note when the condition is checked and when the state changes.
  • The structured block controls error handling or resource lifetime, including what happens when an operation fails.
  • The print calls display the results. Compare these results with the method’s documented return type and the input used.

Expected output:

review documentation

For the demonstrated inputs, Python displays 'review documentation'. This result follows from the operation described above; change one input at a time to understand which rule matters.

What to Notice


Pay attention to the accepted input types, the returned value and the boundary behavior. Predict what changes if you replace the example input with an empty, zero, negative or missing value, where that value is meaningful for this operation.

If the task uses a library function, inspect its documented exceptions and return type before reusing it inside a larger function.

Where You Would Use This


This is useful for scripts, maintenance commands and automated workflows. Treat inputs as untrusted, keep normal results separate from errors, and choose exit codes consistently. Keep reusable logic outside the command-line parser so it can be tested independently.

Practice Exercise


Extend this self-contained task helper to persist items to a temporary JSON file.

Before running your edited example, write down the result you expect. Then test at least one valid boundary or deliberately invalid value, using appropriate exception handling where necessary.

Reference: official Python documentation for the complete API contract and version notes.

Common Beginner Mistakes


  • Overwriting the task file before loading it or assuming two simultaneous writers cannot lose updates.
  • Treating successful output as proof of a successful exit code, or accepting an unchecked path or command argument.

Key Takeaways


  • A command-line to-do application can store tasks in JSON and expose add and list commands through argparse.
  • Extend this self-contained task helper to persist items to a temporary JSON file.
Share this resourceSend this page to someone who may find it useful.