Skip to content

Managing Paths with pathlib

pathlib gives you:

  • operator-free path joining
  • clear methods (exists, is_file, mkdir, read_text)
  • cross-platform behavior
pathlib_basics.py
from pathlib import Path
 
base = Path.home() / "Downloads"
print(base)
 
for f in base.iterdir():
    if f.is_file():
        print(f.name, f.suffix)
pathlib_read_write.py
from pathlib import Path
 
p = Path("hello.txt")
p.write_text("hello\n", encoding="utf-8")
print(p.read_text(encoding="utf-8"))

Exercise 2 – Join Paths with os.path.join

Section titled “Exercise 2 – Join Paths with os.path.join”

pch.coffeeTagline

pch.coffeeCta

pch.feedbackHeading

pch.feedbackSubheading