Level 1 / Project 12 - File Extension Counter¶
Home: README
Learn Your Way¶
| Read | Build | Watch | Test | Review | Visualize | Try |
|---|---|---|---|---|---|---|
| Concept | This project | — | Quiz | Flashcards | — | Browser |
Estimated time: 35 minutes
Focus¶
- directory scanning and grouped counts
Why this project exists¶
Count file extensions from a list of paths (or by scanning a directory), then display the distribution with percentages and a bar chart. You will learn Path.suffix, rglob() for recursive scanning, and grouped counting.
Run (copy/paste)¶
Use <repo-root> as the folder containing this repository's README.md.
cd <repo-root>/projects/level-1/12-file-extension-counter
python project.py --input data/sample_input.txt
pytest -q
Expected terminal output¶
=== File Extension Counter ===
.py 5 files (50%) ##########
.txt 3 files (30%) ######
.md 2 files (20%) ####
10 files, 3 unique extensions
4 passed
Expected artifacts¶
data/output.json- Passing tests
- Updated
notes.md
Checkpoint: Baseline code runs and all tests pass. Commit your work before continuing.
Alter it (required) — Extension¶
- Add a
--filterflag that shows only specific extensions (e.g.--filter .py,.txt). - Add a "hidden files" category for files starting with
.(like.gitignore). - Re-run script and tests.
Break it (required) — Core¶
- Point
--dirat an empty directory -- doescount_extensions()return an empty dict or crash? - Use a file list with files that have double extensions like
archive.tar.gz-- which extension is counted? - Point
--dirat a path that does not exist -- does it raiseNotADirectoryError?
Fix it (required) — Core¶
- Handle the empty-directory case by returning an empty dict with a "(no files found)" message.
- Ensure
NotADirectoryErroris raised with the path in the message for non-existent directories. - Add a test for the empty-directory case.
Checkpoint: All modifications done, tests still pass. Good time to review your changes.
Explain it (teach-back)¶
- Why does
count_extensions()userglob("*")instead ofiterdir()? - What does
Path.suffixreturn for a file likearchive.tar.gz? (Just.gz-- not.tar.gz.) - Why normalise extensions to lowercase with
.lower()? - Where would extension counting appear in real software (disk usage analysis, build systems, file managers)?
Mastery check¶
You can move on when you can: - run baseline without docs, - explain one core function line-by-line, - break and recover in one session, - keep tests passing after your change.
Related Concepts¶
- Collections Explained
- Files and Paths
- Functions Explained
- How Imports Work
- Quiz: Collections Explained
| ← Prev | Home | Next → |
|---|---|---|