How do I list all files of a directory?
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.
Introduction
Listing all files in a directory sounds simple, but the best answer depends on whether you want only the current directory, a recursive walk, or file names filtered by type. The most common solutions are command-line tools such as find and language APIs such as Python's pathlib or os.scandir.
Command-Line Listing
If you just need a quick shell answer, use a standard file listing tool. For the current directory only:
That prints one entry per line, but it includes both files and directories. If you want only files recursively, find is usually the better tool:
This starts at the current directory and prints every regular file below it. That is often the cleanest command-line answer because it avoids guessing which entries are directories and which are files.
If you want a different starting directory, replace . with the target path:
Using Python With pathlib
For application code, pathlib is a strong default because it is readable and cross-platform.
To list only direct child files:
iterdir() lists the immediate contents of the directory. It does not recurse into subdirectories.
If you want recursive listing, use rglob:
This is convenient when you need to search nested folders without writing recursion manually.
Using os.scandir for Efficiency
os.scandir is another good option, especially when performance matters. It yields directory entries that already know some metadata, so checking whether an entry is a file can be efficient.
This is a strong choice when you want a fast direct-directory scan and do not need the richer object API of pathlib.
Decide Whether You Need Recursion
Many bugs come from not being explicit about whether subdirectories should be included. These are different tasks:
- list direct children only,
- list all files recursively,
- list files matching a pattern.
For example, if you only want Python files below a directory:
Choosing the right API is mostly about choosing the right traversal depth and filter.
Handle Errors Deliberately
In real applications, directories may contain unreadable locations, broken links, or race conditions where files disappear during traversal. For simple scripts, that may not matter. For production code, handle those cases explicitly instead of assuming the directory tree is stable.
Even a basic existence check can improve clarity:
Common Pitfalls
- Using
lswhen you actually need only files and not directories. - Forgetting to decide whether the listing should be recursive.
- Building manual recursion when
findorpathlib.rglobalready solves the problem cleanly. - Assuming every path is readable and stable during traversal.
- Printing only file names when the calling code actually needs full paths.
Summary
- Use
findon the command line when you need a quick recursive file listing. - Use
pathlibin Python for clear and cross-platform directory traversal. - Use
os.scandirwhen you want a simple and efficient direct scan. - Be explicit about whether you want recursive results or only immediate children.
- Decide early whether the caller needs file names, full paths, or filtered file types.
Related reading
- How do I list all the columns in a table?
- How do I loop through a list by twos?
- How do I make a exact duplicate copy of an array?
- How do I make a flat list out of a list of lists?
- How do I merge a list of dicts into a single dict?
- How do I merge multiple lists into one list?
- How do I merge two dictionaries in a single expression in Python?
- How do I merge two lists of tuples based on a key?

DSA Fundamentals
Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.