This Python tutorial explains fnmatch.filter() 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
Filter a list of names by wildcard pattern using fnmatch.filter().
fnmatch.filter(names, pattern) returns a list containing matching items. It does not open directories, recurse through the filesystem, or inspect file contents.
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 fnmatch
candidate_files = ["alpha.json", "beta.txt", "gamma.json"]
selected_files = fnmatch.filter(candidate_files, "*.json")
print(selected_files)
Understanding the Code
- First, the imports make fnmatch available. They provide the filesystem, data or standard-library functions used later.
- Find the call fnmatch.filter(candidate_files, "*.json"). Its parameters determine the specific operation demonstrated in the title.
- Its purpose is to filter a list of names by wildcard pattern using fnmatch.filter(). Compare this description against the result printed by the program.
- The print() expressions display a checkable outcome. Print values and exception names are not substitutes for the program result itself.
Expected Output
['alpha.json', 'gamma.json']
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 when the goal is to locate paths in the filesystem.
fnmatch.filter() matches filenames against patterns, not directory contents recursively.
Practice: Try It Yourself
Filter a list of log filenames ending in .log.
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
- Assuming filter() takes a directory path instead of an iterable of names.
- Using filename glob patterns to inspect file contents or metadata.
- fnmatch.filter() matches filenames against patterns, not directory contents recursively.
Key Takeaways
- Filter a list of names by wildcard pattern using fnmatch.filter().
- fnmatch.filter(names, pattern) returns a list containing matching items. It does not open directories, recurse through the filesystem, or inspect file contents.
- Avoid this common pitfall: Assuming filter() takes a directory path instead of an iterable of names.