Use glob when a directory contains many files but your program should process only names matching a shell-style pattern. Unlike os.listdir, glob.glob returns matching pathnames with their directory prefixes, avoiding separate path construction. section 6.17
A practical glob workflow is:
- Choose a directory and pattern, such as *.conf or *.json. section 6.17 section 6.23
- Call glob.glob(pattern) and iterate over the returned filename strings. section 6.23
- Open or otherwise process each selected file without manually adding the directory name. section 6.17
Collect JSON score files
import glob for filename in glob.glob('scores/*.json'): process(filename) section 6.23
filenames = glob.glob('/etc/*.conf')
Compare the compact glob.glob call with os.listdir, then consider pathlib.Path.glob when Path objects are more useful than strings. section 6.17


