typestar

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

  1. / joins path segments cleanly.
  2. name, suffix, and parent decompose a path.
  3. glob finds files by pattern; resolve makes it absolute.

Keywords and builtins used here

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.

Type this snippet

Step 7 of 7 in Iterators, errors & files, step 50 of 72 in Language basics.

← Previous Next →