Skip to content

Pattern Matching with glob

diagram the three things glob quietly does not do mermaid
A pattern that looks like it should match everything usually does not. Two stars only recurse if you ask for recursion, a leading dot has to be matched explicitly, and case sensitivity depends on the filesystem rather than on your pattern. All three were measured on the same small tree.

glob finds files using wildcard patterns:

  • *.txt
  • data_??.csv
  • recursive patterns with **
glob_basic.py
from glob import glob
 
for path in glob("*.py"):
    print(path)
glob_recursive.py
from glob import glob
 
for path in glob("**/*.md", recursive=True):
    print(path)

pathlib.Path.rglob is often cleaner.

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