
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.txtgoes totxt/notes.txttides.CSVgoes tocsv/tides.CSV(the folder is lowercase; the file keeps its name)READMEhas no suffix, so it goes tomisc/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
plan_moves(root)returns a tuple(moves, skipped):movesis a dict{name: "folder/name"}for each file directly insideroot. 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
movesand put the name inskippedinstead.skippedis a sorted list. - It must not change anything on disk.
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.
plan_moves(root)returns a tuple(moves, skipped):movesis a dict{name: "folder/name"}for each file directly insideroot. 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
movesand put the name inskippedinstead.skippedis a sorted list. - It must not change anything on disk.
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.
main.py Python sleeping
Press Run to try your code, then Check to test it.