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

40. Build Tools for Repeated Tasks · Build a File Organisation Planner

Process a list of filenames and flag two files that would target the same destination.

Lesson files · lessons/command-line-tools/file-sort-plan

Before you start
- Plan a destination using a path’s suffix and name.
- Use casefold() for text comparison and a set to track distinct values.

By the end
- Generate destinations for a list of filenames in input order.
- Use the sample paths to identify the two destinations that collide after casefolding.

Where you can use it
Preview the proposed folder contents and flag report names that would clash when capitalisation is ignored.

Make a tool someone can trust with a folder list. Read a count and that many paths. For each path, calculate the destination before taking any action. To spot copies that would conflict on case-insensitive filesystems, compare casefolded destinations and print a conflict message.

Predict
Will those two destinations collide under a case-insensitive comparison?
- No.
- Yes.

Worked example (example.py)

destinations = ["tables/Report.csv", "tables/report.csv"]
print(len({item.casefold() for item in destinations}))


Example result
1

Read a count followed by filenames. Route .csv to tables/, .txt to text/, and others to other/. Print filename → destination for new destinations, or Conflict: filename when a destination was already seen (ignoring case).

Starter program (starter.py)

from pathlib import PurePosixPath
for _ in range(int(input())):
    path = PurePosixPath(input())
    print(path.name)


Starter input
3
folderA/Report.csv
folderB/report.CSV
notes.txt

Hint
Use a set for destinations you have already planned. Compare destination.casefold() before adding it.

Case 1. Try the sample
Input
3
folderA/Report.csv
folderB/report.CSV
notes.txt
Expected output
Report.csv → tables/Report.csv
Conflict: report.CSV
notes.txt → text/notes.txt

Case 2. Other files
Input
1
map.png
Expected output
map.png → other/map.png

Project checklist
Run the sample case.

Project checklist
Try one different case and explain what happened.

Marking criterion
Program behaviour (4 points)
Each required behaviour works on the supplied cases.

Marking criterion
Use of the lesson concept (3 points)
The code uses the main idea in a clear, purposeful way.

Marking criterion
Test evidence (2 points)
The reflection describes a case, its expected result and what happened.

Marking criterion
Explanation (1 points)
The learner explains a change or design decision in their own words.

Stretch
Add a heading showing how many actions and conflicts the plan contains.

Reflect
What test would catch deleting or overwriting a source file in a future version of this tool?

Check your prediction
Yes. Both casefold to tables/report.csv.
