Software Engineer (SDE)pythonbeginner3 interactive MCQs in app
os & pathlib Modules
Path / cross-platform joining & mkdir(parents=True, exist_ok=True)path traversal prevention via .resolve() + is_relative_to()Path("/a") / "/b" root overwrite pitfall
Core Concept
Use `pathlib.Path` over fragile string concatenation (`folder + "/" + file`): `/` operator (`Path("data") / "2026" / "out.csv"`) normalizes separators cleanly across Windows (`\`) and Linux (`/`), while `.stem`, `.suffix`, `.parent`, `.resolve()`, `.mkdir(parents=True, exist_ok=True)`, and `.read_text(encoding="utf-8")` replace nested `os.path` calls cleanly!
Worked Example
`out = Path("reports") / f"{slug}.csv"; out.parent.mkdir(parents=True, exist_ok=True)` builds cross-platform nested folders in two lines cleanly!
Common Pitfall
(SECURITY CRITICAL!) joining untrusted filenames (`"../../etc/passwd"`) with `base / filename` jumps OUTSIDE `base` cleanly (`Path("/uploads") / "/etc/passwd"` evaluates straight to `"/etc/passwd"`!). Always validate `target = (base / name).resolve(); target.is_relative_to(base.resolve())`!
Recommended Readings & Practice
Practice os & pathlib Modules with 3 graded MCQs & written feedback.
Take SOARDO’s free 12-question diagnostic to see how strong you are across your target role and unlock adaptive daily missions.