all Python 3fnmatchfunction
fnmatch.filter()
filename patterns
Returns a list of the names from an iterable that match the pattern: the same as [n for n in names if fnmatch(n, pat)], only faster.
Syntax
fnmatch.filter(names, pat)
The pattern is compiled once, so on large lists this is noticeably faster than a loop calling fnmatch(). Case follows the OS rules, as with fnmatch(). The order of names is kept and the disk is never read: to find files, first get their names with os.listdir(), or use glob.
Examples
import fnmatch
names = ['main.py', 'notes.txt', 'test_main.py', '.hidden.py']
print(fnmatch.filter(names, '*.py')) # → ['main.py', 'test_main.py', '.hidden.py']
print(fnmatch.filter(names, 'test_*')) # → ['test_main.py']
See also
Open in the full glossarydocs.python.orgReport an inaccuracy