Python
glob module
file handling
multiple filetypes
programming tutorial

Python glob multiple filetypes

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions

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.

python
1import glob
2
3patterns = ["images/*.jpg", "images/*.png", "images/*.gif"]
4files = []
5
6for pattern in patterns:
7    files.extend(glob.glob(pattern))
8
9print(files)

This is easy to understand and works well when the extension set is small.

You can also write it as a comprehension:

python
1import glob
2
3patterns = ["images/*.jpg", "images/*.png", "images/*.gif"]
4files = [path for pattern in patterns for path in glob.glob(pattern)]
5print(files)

Use pathlib for Richer Filtering

For many modern Python codebases, pathlib is more expressive than raw glob strings.

python
1from pathlib import Path
2
3extensions = {".jpg", ".png", ".gif"}
4image_dir = Path("images")
5
6files = [p for p in image_dir.iterdir() if p.is_file() and p.suffix.lower() in extensions]
7print(files)

This is often cleaner when you want Path objects and expect the filtering rules to grow later.

If files may exist in subdirectories, use recursive traversal.

python
1from pathlib import Path
2
3extensions = {".jpg", ".png", ".gif"}
4files = [
5    p for p in Path("images").rglob("*")
6    if p.is_file() and p.suffix.lower() in extensions
7]
8print(files)

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:

python
1import glob
2
3patterns = ["images/*.*", "images/*.jpg"]
4files = sorted(set(path for pattern in patterns for path in glob.glob(pattern)))
5print(files)

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 Path objects
  • 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:

python
1from pathlib import Path
2
3def find_by_suffixes(root: str, suffixes: set[str]) -> list[Path]:
4    base = Path(root)
5    return sorted(
6        p for p in base.rglob("*")
7        if p.is_file() and p.suffix.lower() in suffixes
8    )

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.'
  • 'pathlib is often cleaner when you want Path objects 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
Free course
Beginner
7 lessons
2 hours
Tackling System Design Interview Problems

A short course that equips you with the skills to approach system design interviews methodically.

Start the free course
Track 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.

Browse interview questions

All Rights Reserved.