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.
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.
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:
Recursive Globbing with **
If you want to search subdirectories too, use ** with recursive=True.
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.
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.
For recursive matching, use rglob:
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.
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.
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.globwhen you want a list of file paths matching a shell-style pattern. - Use
glob.iglobwhen the result set may be large and iteration is enough. - Use
pathlib.Path.globorrglobwhen you wantPathobjects and cleaner path handling. - Add
recursive=Truewhen using**with theglobmodule. - Sort or filter the matches explicitly when order and file type matter.
Related reading
- Getting a list of values from a list of dicts
- Getting a map to return a list in Python 3.x
- Getting a prediction from an ONNX model in python
- Getting an unexpected NameError in pycassaShell when invoking one function from another
- Getting attributes of a class
- Getting error Could not import PIL.Image. The use of array_to_img requires PIL.
- Getting error while plotting the dendrogram for the spearmanr correlation
- Getting file size in Python?
.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.