──────────────────────────────────────

38. Build Tools for Repeated Tasks · Preview a File-Sorting Plan

Generate a repeatable list of proposed destinations for sample filenames.

Lesson files · lessons/command-line-tools/dry-run-plan

Before you start
- Read the name and suffix from a path.
- Look up a value in a dictionary with a default.

By the end
- Print the proposed destination for a text file, a CSV or an unfamiliar file type.
- Explain how the preview lets a reader catch an incorrect destination before moving a file.

Where you can use it
Review a printed destination list before organising a shared folder of notes, tables and other files.

A dry run tells the person what the tool would do, so they can catch a mistake before files are changed. Keep plan creation separate from any file operation. Use base names to report a consistent destination, and test unfamiliar suffixes.

Predict
What should a dry run do when it sees notes.TXT?
- Move the file immediately.
- Print the proposed destination.

Worked example (example.py)

from pathlib import PurePosixPath
path = PurePosixPath("class/notes.TXT")
print(f"{path.name} → text/{path.name}")


Example result
notes.TXT → text/notes.TXT

Read a filename. Print its filename, an arrow →, and a destination. Send .txt to text/, .csv to tables/, and everything else to other/. Preserve the original filename on both sides.

Starter program (starter.py)

from pathlib import PurePosixPath
path = PurePosixPath("class/notes.TXT")
print(f"{path.name} → text/{path.name}")


Hint
Use a dictionary of folders keyed by lowercase suffix and get(..., "other") for the fallback.

Case 1. Try the sample
Input
survey.CSV
Expected output
survey.CSV → tables/survey.CSV

Case 2. Unknown suffix
Input
drawing.svg
Expected output
drawing.svg → other/drawing.svg

Case 3. Route a note
Input
workshop/Notes.TXT
Expected output
Notes.TXT → text/Notes.TXT

Stretch
Print a sorted plan for five filenames and identify a possible destination collision.

Reflect
How does seeing the plan help you spot a wrongly classified file?

Check your prediction
Print the proposed destination. The preview lets the learner verify changes first.
