file management
glob module
directory listing
Python
programming

Getting a list of files in a directory with a glob

Interview Questions practice on Codemia

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

Browse interview questions

Introduction

In Python, a glob is a shell-style pattern used to match file paths. It is a convenient way to list files such as all .txt files, all images in nested folders, or filenames matching a numbered pattern.

The standard library gives you two common choices: the glob module and pathlib.Path.glob. Both are useful, and the right one depends mostly on whether you want plain strings or Path objects.

Basic glob.glob Usage

The most direct approach is glob.glob(pattern), which returns a list of matching paths.

python
1import glob
2
3files = glob.glob("data/*.csv")
4print(files)

This matches every .csv file directly under the data directory.

Common wildcard patterns are:

  • '* for any sequence of characters'
  • '? for a single character'
  • '[abc] for one character from a set'

For example:

python
1import glob
2
3print(glob.glob("logs/app-?.txt"))
4print(glob.glob("images/*.[jp][pn]g"))

Recursive Globbing with **

If you want to search subdirectories too, use ** with recursive=True.

python
1import glob
2
3python_files = glob.glob("project/**/*.py", recursive=True)
4print(python_files)

Without recursive=True, the double-star pattern does not behave as a recursive traversal in the way most people expect.

This is one of the most common reasons a glob seems to “miss” files.

Use glob.iglob for Large Results

glob.glob builds the full list in memory. If the match set may be large, glob.iglob is often better because it returns an iterator.

python
1import glob
2
3for path in glob.iglob("project/**/*.py", recursive=True):
4    print(path)

This is useful for streaming large directory walks instead of materializing everything at once.

pathlib.Path.glob and rglob

If you prefer object-oriented path handling, pathlib is often more pleasant.

python
1from pathlib import Path
2
3base = Path("data")
4for path in base.glob("*.csv"):
5    print(path, path.name)

For recursive matching, use rglob:

python
1from pathlib import Path
2
3for path in Path("project").rglob("*.py"):
4    print(path)

pathlib is especially convenient when you want to do more with the matched paths afterward, such as checking metadata or reading files.

Filtering to Files Only

Globs can match directories too, depending on the pattern. If you truly want files only, filter the result.

python
1from pathlib import Path
2
3matches = [p for p in Path("project").rglob("*") if p.is_file()]
4print(matches)

This is useful when the pattern is broad and the distinction between files and directories matters.

Sorting the Results

Glob results are not always guaranteed in the order you want. If order matters, sort explicitly.

python
1import glob
2
3files = sorted(glob.glob("data/*.csv"))
4print(files)

This keeps downstream processing deterministic, which is often important in scripts and data pipelines.

When Glob Is the Wrong Tool

Glob works well for filename patterns. It is less appropriate when you need filtering based on file size, modification time, or more advanced rules. In those cases, os.scandir, pathlib, or os.walk combined with your own logic may be better.

Glob is best when the file-selection rule is primarily a path pattern.

Common Pitfalls

One common mistake is expecting ** to recurse without enabling recursive behavior. With the glob module, recursive matching needs recursive=True.

Another issue is assuming glob results are automatically sorted. If processing order matters, sort the matches yourself.

It is also easy to forget that globs are path patterns, not regular expressions. The syntax is simpler and more limited than regex syntax.

Finally, be careful with very broad recursive patterns in large directory trees. They can match a huge number of paths and slow down scripts unexpectedly.

Summary

  • Use glob.glob when you want a list of file paths matching a shell-style pattern.
  • Use glob.iglob when the result set may be large and iteration is enough.
  • Use pathlib.Path.glob or rglob when you want Path objects and cleaner path handling.
  • Add recursive=True when using ** with the glob module.
  • Sort or filter the matches explicitly when order and file type matter.

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