When a program must process selected files rather than every directory entry, glob finds pathnames matching shell-style patterns such as *.json or *.conf. section 5.27 section 5.20
Use glob when:
- You need files with a particular suffix, such as JSON or configuration files. section 5.27 section 5.20
- You want returned strings to include the directory path, avoiding separate path construction. section 5.20
- You prefer a pattern-based alternative to listing everything with os.listdir. section 5.20
Collect JSON files
import glob for filename in glob.glob('scores/*.json'): print(filename) The pattern selects JSON files inside scores, and each iteration receives a matching filename string. Those files can then be opened and processed, such as by loading a JSON array with json.load. section 5.27
filenames = glob.glob('/etc/*.conf')
Compare the concise pattern-based call with os.listdir, which returns unfiltered directory entries without the directory name. section 5.20


