Checkpoint

Checkpoint: The Archive Organizer

The Guild's inbox is a jumble. Pip wants every file moved into a subfolder named after its type:

  • notes.txt goes to txt/notes.txt
  • tides.CSV goes to csv/tides.CSV (the folder is lowercase; the file keeps its name)
  • README has no suffix, so it goes to misc/README

The golden rule: never lose data. If the target already exists, skip that file and report it. Never overwrite.

The moving parts

target = root / "txt" / "notes.txt"
target.exists()            # is something already there?
target.parent.mkdir(parents=True, exist_ok=True)
(root / "notes.txt").rename(target)   # move it

mkdir(parents=True, exist_ok=True) makes the folder (and any missing parents) and doesn't complain if it already exists.

Assignment

  1. plan_moves(root) returns a tuple (moves, skipped):
    • moves is a dict {name: "folder/name"} for each file directly inside root. The folder is the lowercase suffix without the dot, or "misc" if there's no suffix.
    • If the target already exists, leave it out of moves and put the name in skipped instead. skipped is a sorted list.
    • It must not change anything on disk.
  2. apply_moves(root, moves) makes each needed folder and moves each file. Check again just before each move. If the target exists by then, leave that file where it is. Return how many files moved.