Level 1 / Project 08 - Path Exists Checker¶
Home: README
Learn Your Way¶
| Read | Build | Watch | Test | Review | Visualize | Try |
|---|---|---|---|---|---|---|
| Concept | This project | — | Quiz | Flashcards | — | Browser |
Estimated time: 30 minutes
Focus¶
- filesystem existence and type checks
Why this project exists¶
Check whether file paths exist, determine if each is a file or directory, and report sizes in human-readable format. You will learn pathlib.Path methods like exists(), is_file(), is_dir(), and stat().
Run (copy/paste)¶
Use <repo-root> as the folder containing this repository's README.md.
cd <repo-root>/projects/level-1/08-path-exists-checker
python project.py --input data/sample_input.txt
pytest -q
Expected terminal output¶
=== Path Checker ===
data/sample_input.txt EXISTS file 0.1 KB
data/output.json EXISTS file 0.3 KB
/nonexistent/path/file MISSING -- --
2/3 paths exist
5 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 "last modified" field showing when each file was last changed (use
os.path.getmtime()). - Add a
--exists-onlyflag that only shows paths that actually exist. - Re-run script and tests.
Break it (required) — Core¶
- Add a path with special characters like
data/my file (1).txt-- doescheck_path()handle spaces in paths? - Add a symbolic link (if on Linux/Mac) or a path to a network drive -- does it detect the type correctly?
- Add a very deeply nested path that does not exist -- does the checker handle long paths gracefully?
Fix it (required) — Core¶
- Ensure
check_path()works with paths containing spaces by usingPathobjects consistently. - Handle permission errors (e.g. restricted directories) by catching
PermissionError. - Add a test for the missing-path case.
Checkpoint: All modifications done, tests still pass. Good time to review your changes.
Explain it (teach-back)¶
- What does
Path.exists()do and why is it better thanos.path.exists()? - Why does
format_size()convert bytes to KB/MB/GB instead of just showing raw bytes? - What is the difference between
is_file(),is_dir(), andexists()on aPathobject? - Where would path checking appear in real software (deployment scripts, backup tools, 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¶
| ← Prev | Home | Next → |
|---|---|---|