Python Get All Files in Folder: A Complete Guide with Practical Examples
Working with files and directories is one of the most fundamental tasks in Python programming. Whether you are building a data processing pipeline, automating file management, or developing a web application that handles user uploads, knowing how to Python get all files in folder is an essential skill. This thorough look walks you through every method available, complete with code examples, best practices, and expert tips that will elevate your scripting abilities Took long enough..
Introduction
Python provides multiple powerful modules for interacting with the file system. When you need to list all files in a directory, the approach you choose depends on your specific requirements — do you need only files in the top-level folder, or do you also want files nested inside subdirectories? Do you need to filter by file extension? Understanding the strengths and limitations of each method will help you write cleaner, more efficient code That alone is useful..
In this article, we will explore the most commonly used techniques including os.listdir(), os.On top of that, scandir(), os. Because of that, walk(), glob. glob(), and the modern pathlib module. Each method has its own use case, and by the end of this guide, you will know exactly which one to reach for in any scenario Less friction, more output..
Why Knowing How to List Files in a Folder Matters
Before diving into the code, it is worth understanding why this topic is so important in real-world development:
- Batch Processing: When you need to process hundreds or thousands of files at once, such as converting image formats or parsing CSV documents.
- File Organization: Automating the sorting and categorization of files based on type, date, or size.
- Data Science Workflows: Loading datasets stored across multiple directories for analysis and machine learning tasks.
- System Administration: Monitoring directories for new files, generating directory reports, or cleaning up temporary folders.
Mastering these techniques gives you the foundation to handle all of these situations with confidence Simple, but easy to overlook. And it works..
Method 1: Using os.listdir()
The os.listdir() function is one of the simplest and most straightforward ways to get all files in a folder in Python. It returns a list containing the names of all entries — both files and directories — in the specified path That's the whole idea..
import os
folder_path = "/path/to/your/folder"
entries = os.listdir(folder_path)
for entry in entries:
print(entry)
This method is beginner-friendly and works well for basic listing tasks. That said, it has a notable limitation: it does not distinguish between files and directories. Every entry is returned as a plain string, so you need an additional check if you want only files That's the whole idea..
Filtering Only Files with os.listdir()
To get only files and exclude subdirectories, you can combine os.That said, listdir() with `os. path Most people skip this — try not to..
import os
folder_path = "/path/to/your/folder"
files_only = [f for f in os.Practically speaking, path. Now, isfile(os. listdir(folder_path) if os.path.
for file in files_only:
print(file)
This is a widely used pattern in Python scripting and works reliably across different operating systems.
Method 2: Using os.scandir()
Introduced in Python 3.5, os.listdir(). Day to day, scandir()is a more efficient alternative toos. Instead of returning plain strings, it returns an iterator of DirEntry objects, which contain rich metadata about each entry, such as file type, size, and modification time — all without requiring additional system calls That's the part that actually makes a difference..
import os
folder_path = "/path/to/your/folder"
with os.scandir(folder_path) as entries:
for entry in entries:
if entry.is_file():
print(f"File: {entry.name}, Size: {entry.stat().
### Why `os.scandir()` Is Preferred Over `os.listdir()`
The key advantage of `os.scandir()` is **performance**. When you need to check file types or access metadata, `os.scandir()` retrieves this information during the directory scan itself, avoiding the overhead of separate `stat()` system calls for each entry. This makes it significantly faster when working with directories containing thousands of files.
## Method 3: Using `os.walk()` for Recursive File Listing
When you need to **get all files in a folder and its subfolders**, `os.walk()` is the go-to solution. This function generates the file names in a directory tree by walking the tree either top-down or bottom-up, yielding a tuple of three values for each directory it visits: the directory path, a list of subdirectories, and a list of files.
```python
import os
folder_path = "/path/to/your/folder"
all_files = []
for dirpath, dirnames, filenames in os.Day to day, walk(folder_path):
for filename in filenames:
full_path = os. So path. join(dirpath, filename)
all_files.
print(f"Total files found: {len(all_files)}")
for f in all_files:
print(f)
Understanding the os.walk() Output
Each iteration of the os.walk() loop provides:
dirpath: The path to the current directory being scanned.dirnames: A list of subdirectory names withindirpath.filenames: A list of non-directory file names withindirpath.
This makes os.walk() incredibly powerful for tasks like searching for specific file types across an entire project directory or building an index of all files in a document repository.
Method 4: Using glob.glob() for Pattern Matching
The glob module is perfect when you want to list files in a directory that match a specific pattern. It uses Unix shell-style wildcards, making it intuitive and expressive.
import glob
# Get all CSV files in a folder
csv_files = glob.glob("/path/to/your/folder/*.csv")
# Get all text files
txt_files = glob.glob("/path/to/your/folder/*.txt")
# Get all files (all types)
all_files = glob.glob("/path/to/your/folder/*")
for f in csv_files:
print(f)
Recursive Globbing with glob
Starting from Python 3.5, glob.glob() supports the ** wildcard for recursive matching:
import glob
# Get all Python files in folder and all subfolders
py_files = glob.glob("/path/to/your/folder/**/*.py", recursive=True)
for f in py_files:
print(f)
This is an elegant way to get all files in a folder recursively while filtering by extension, eliminating the need for manual directory traversal.
Method 5: Using pathlib.Path — The Modern Approach
pathlib is the most modern and Pythonic way to interact with file paths. Practically speaking, introduced in Python 3. 4, it provides an object-oriented interface that makes file system operations more readable and intuitive.
from pathlib import Path
folder_path = Path("/path/to/your/folder")
# Get all files in the folder (non-recursive)
all_files = [f for f in folder_path.iterdir() if f.is_file()]
for
```python
from pathlib import Path
folder_path = Path("/path/to/your/folder")
# Get all files in the folder (non-recursive)
all_files = [f for f in folder_path.iterdir() if f.is_file()]
for f in all_files:
print(f)
Recursive Search with pathlib
Just like glob, pathlib offers a concise way to walk directories recursively:
# Recursively find all .py files
py_files = folder_path.rglob("*.py")
for f in py_files:
print(f)
rglob behaves like glob.glob(...That's why , recursive=True) but returns Path objects, letting you take advantage of the rich API (e. g.In practice, , f. Worth adding: stat(), f. parent, f.suffix) without extra conversion.
Filtering and Transforming Results
Because pathlib yields objects, you can chain filters naturally:
# Find all files larger than 1 MB that were modified in the last 7 days
import time
now = time.time()
one_week_ago = now - 7 * 86400
large_recent = [
f for f in folder_path.Because of that, st_size > 1_048_576 and f. is_file() and f.stat().So rglob("*")
if f. stat().
for f in large_recent:
print(f, f.stat().st_size)
Advantages of pathlib
- Object‑oriented: No need to constantly call
os.path.joinoros.path.splitext. - Cross‑platform: Handles Windows and POSIX separators transparently.
- Readability: Intent is clear at a glance (
folder_path.rglob("*.txt")). - Chainability: Easy to combine with other Python idioms (list comprehensions, generator expressions,
filter,map).
Choosing the Right Method
| Method | Recursive? | Pattern Support | Returns | Best Use Case |
|---|---|---|---|---|
os.listdir() |
No | No | str |
Simple, non‑recursive listing |
os.scandir() |
No | No | DirEntry objects |
Performance‑sensitive scans |
os.Still, walk() |
Yes (manual) | No | tuples (dirpath, dirnames, filenames) |
Full tree traversal when you need dirs & files |
glob. glob() |
Yes (with **) |
Unix‑style wildcards | str |
Quick pattern‑based searches |
| `pathlib. |
For most new projects, pathlib strikes the best balance between readability and functionality. If you need the absolute fastest walk and only care about file names, os.scandir() (or os.walk()) remains a solid low‑level choice. When you just want a quick glob pattern match, glob (especially with the recursive flag) is unbeatable in brevity.
Conclusion
Listing files in a directory is a common task, and Python offers several complementary tools to accomplish it—from the classic os module functions to the expressive glob patterns and the modern, object‑oriented pathlib API. Whether you’re building a simple script or a large‑scale data‑processing pipeline, these techniques give you the flexibility to traverse and filter filesystems efficiently and cleanly. In practice, by understanding the strengths and trade‑offs of each approach, you can select the method that best fits your project’s performance needs, readability goals, and complexity. Happy coding!