Python glob multiple filetypes
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
When people ask how to glob multiple file types in Python, they usually want one file list containing several extensions such as .jpg, .png, and .gif. In portable Python code, the usual answer is to combine multiple patterns or iterate once and filter by suffix.
Combine Several glob Patterns
The simplest approach is to call glob once per pattern and merge the results.
This is easy to understand and works well when the extension set is small.
You can also write it as a comprehension:
Use pathlib for Richer Filtering
For many modern Python codebases, pathlib is more expressive than raw glob strings.
This is often cleaner when you want Path objects and expect the filtering rules to grow later.
Recursive Search
If files may exist in subdirectories, use recursive traversal.
This is usually easier to reason about than trying to manage several recursive wildcard strings manually.
Case Sensitivity and Duplicates
Uppercase extensions such as .JPG matter on some filesystems, so normalizing with suffix.lower() is a good habit.
If patterns overlap, deduplicate the result:
This prevents repeated entries when the same file matches more than one pattern.
When a Single Brace Pattern Is Not Ideal
Developers sometimes expect shell-style brace expansion such as *.{jpg,png,gif} to work automatically. That is not the most portable assumption in Python code.
Explicit multiple patterns or suffix filtering is usually clearer and more predictable.
Which Style Should You Prefer
Use repeated glob.glob() calls when:
- the file types are few
- the rule is pattern-oriented
- you want the simplest direct script
Use pathlib when:
- you want
Pathobjects - filtering logic may become richer
- recursive traversal is important
The right choice is more about maintainability than about micro-optimizing performance.
A Small Reusable Helper
If you do this often, wrapping the logic in one helper keeps sorting and deduplication consistent:
Common Pitfalls
Assuming one shell-style brace pattern works everywhere is a common portability mistake.
Forgetting uppercase extensions can silently miss files on case-sensitive systems.
Combining overlapping patterns without deduplication can produce repeated entries.
Using glob patterns when the real selection rule needs richer file inspection makes the code harder to extend later.
Summary
- The usual Python answer is to combine several glob patterns or filter paths by suffix.
- '
glob.glob()is simple and works well for small extension sets.' - '
pathlibis often cleaner when you wantPathobjects and richer filtering.' - Normalize extension case and deduplicate results when patterns overlap.
- Pick the style that matches how complex your file-selection rule actually is.
Related reading
- Python Graph Library
- Python, how to determine if an object is iterable?
- Python how to determine if an object is iterable?
- Python How to find Accuracy Result in SVM Text Classifier Algorithm for Multilabel Class
- Python How to group a list of objects by their characteristics or attributes?
- python how to identify if a variable is an array or a scalar
- Python How to ignore an exception and proceed?
- Python how to mock a kafka topic for unit tests?
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.