Content · Programming · Python

Python glob.iglob() Function: Usage and Examples

Iterate lazily over matching paths with glob.iglob(). glob.iglob(pattern, recursive=False) produces an iterator instead of building a list.

2 views Published Oct 17, 2026 Updated Oct 18, 2026 Admin

This Python tutorial explains glob.iglob() Function: Usage and Examples. Follow the example, inspect the output, and practise an input change so you understand the behavior rather than simply copying the syntax.

What This Feature Does


Iterate lazily over matching paths with glob.iglob().

glob.iglob(pattern, recursive=False) produces an iterator instead of building a list. It can reduce peak memory use for many matches but filesystem traversal still has a runtime cost.

When to Use It


This technique belongs to file copying, temporary files and matching. It is useful when the input, operation and expected outcome are known. Start with a small controlled example before integrating it into a larger workflow.

Step-by-Step Python Example


Read the code once before running it. Notice which values are inputs, which function or method performs the work, and whether the result is returned or stored by changing an existing object.

import glob
from pathlib import Path
from tempfile import TemporaryDirectory

with TemporaryDirectory() as temporary_folder:
    test_root = Path(temporary_folder)
    (test_root / "one.log").touch()
    (test_root / "two.log").touch()
    matched_names = sorted(Path(path_string).name for path_string in glob.iglob(str(test_root / "*.log")))
    print(matched_names)

Understanding the Code


  • First, the imports make glob, pathlib, tempfile available. They provide the filesystem, data or standard-library functions used later.
  • Find the call glob.iglob(str(test_root / "*.log")). Its parameters determine the specific operation demonstrated in the title.
  • Its purpose is to iterate lazily over matching paths with glob.iglob(). Compare this description against the result printed by the program.
  • The sample uses TemporaryDirectory() so any practice files are removed after the context ends; it does not work on your real documents.
  • The print() expressions display a checkable outcome. Print values and exception names are not substitutes for the program result itself.

Expected Output


['one.log', 'two.log']

The output is the observable result of the sample operation. Compare its value and type with the explanation, not just whether the program runs.

Important Details and Edge Cases


Use glob.glob() when you explicitly need the whole list at once.

glob.iglob() yields matches lazily but still may scan large directories and does not promise sorted order.

Practice: Try It Yourself


Iterate over many temporary filenames and count matches.

Begin by copying the example into a separate practice file. Change one input at a time, run it again, and describe why the result changed. If the exercise writes or deletes files, keep it inside a temporary test folder.

Official Python Documentation


For the complete reference, see the Python standard library documentation.

Common Beginner Mistakes


  • Trying to index the iterator returned by iglob() as though it were a list.
  • Assuming iglob() guarantees matching files remain unchanged during iteration.
  • glob.iglob() yields matches lazily but still may scan large directories and does not promise sorted order.

Key Takeaways


  • Iterate lazily over matching paths with glob.iglob().
  • glob.iglob(pattern, recursive=False) produces an iterator instead of building a list. It can reduce peak memory use for many matches but filesystem traversal still has a runtime cost.
  • Avoid this common pitfall: Trying to index the iterator returned by iglob() as though it were a list.
Share this resourceSend this page to someone who may find it useful.