Paths with pathlib in Python
Filesystem paths as objects instead of strings.
from pathlib import Path
project = Path("src") / "app"
config = project / "settings.toml"
name = config.name
suffix = config.suffix
parent = config.parent
exists = config.exists()
scripts = sorted(project.glob("*.py"))
home = Path.home()
absolute = config.resolve()
How it works
/joins path segments cleanly.name,suffix, andparentdecompose a path.globfinds files by pattern;resolvemakes it absolute.
Keywords and builtins used here
sorted
The run, in numbers
- Lines
- 14
- Characters to type
- 269
- Tokens
- 72
- Three-star pace
- 100 tpm
At the three-star pace of 100 tokens a minute, this run takes about 43 seconds.
Step 7 of 7 in Iterators, errors & files, step 50 of 72 in Language basics.