Some text some message..
Back 🔍 .glob() : search filter tool 🎯 -in pathlib.Path 18 Aug, 2025

Think of .glob() as a search filter tool 🎯 that helps you find files and folders matching a pattern inside a directory.


🌟 Basic Usage

from pathlib import Path

folder = Path("documents")

for file in folder.glob("*.txt"):
    print(file)

👉 Finds all files ending with .txt in the documents folder.


🎨 Pattern Matching

  • "*.txt" → 📄 All text files

  • "*.pdf" → 📑 All PDFs

  • "*.jpg" or "*.png" → 🖼️ All images of given type

  • "*.*" → 🌍 Everything with an extension

  • "file_*.csv" → 🔢 Files starting with file_ and ending in .csv


🚀 Recursive Search with .rglob()

for file in folder.rglob("*.txt"):
    print(file)

📂 .rglob() = Searches inside subfolders too (deep search).


🎯 Example: Mixed Filtering

# All Python files in current directory
for pyfile in Path(".").glob("*.py"):
    print("Python file:", pyfile)

# All JPG files in images subfolder
for img in Path("images").glob("*.jpg"):
    print("Image:", img)

🌈 Memory Aid

  • .glob("pattern") → 🔍 Search in current folder

  • .rglob("pattern") → 🌎 Search in all subfolders too

  • Patterns (*, ?, [abc]) → 🎭 Act like wildcards


👉 In short:
.glob() = File finder 🎯,
.rglob() = Deep explorer 🗂️