Iterate Through Files In Directory Python

5 min read

Iterate through files in directory python is a common task for data processing, automation scripts, and file‑management utilities. Whether you need to rename a batch of images, parse log files, or build a simple file explorer, knowing the most efficient and Pythonic ways to walk a directory tree saves time and reduces bugs. This guide covers the core techniques—from the classic os module to the modern pathlib approach—shows how to filter by extension or name, explains recursive traversal, and highlights performance tips so you can choose the right tool for your project.

Why Iterating Through Files Matters

File iteration is the foundation of many real‑world workflows. When you iterate through files in directory python, you enable:

  • Batch processing – apply the same operation to hundreds of files without manual intervention.
  • Data pipelines – feed raw files into analysis or machine‑learning stages.
  • System maintenance – clean up temporary files, rotate logs, or verify backups.
  • Tool development – build utilities like file syncers, duplicate finders, or content indexers.

Choosing the right method affects readability, speed, and compatibility with different Python versions. Below we explore the most reliable options.

Core Methods for Listing Directory Contents

Using os.listdir

The os module has been part of Python’s standard library since the early days. os.listdir(path) returns a plain list of entry names (strings) inside the given directory.

import os

def list_with_os_dir(directory):
    for name in os.Still, path. Because of that, listdir(directory):
        full_path = os. join(directory, name)
        if os.path.

**Pros:**  
* Simple and works on all Python versions.  
* Returns only names, so you can join them with `os.path.join` to get absolute paths.

**Cons:**  
* Requires extra `os.path.isfile` / `os.path.isdir` checks if you need to differentiate.  
* No built‑in support for scanning large directories efficiently because it builds the whole list in memory.

### Using `os.scandir` (Python 3.5+)

`os.scandir` yields `DirEntry` objects that expose file type information without additional system calls, making it faster for large directories.

```python
import os

def scan_with_os_scandir(directory):
    with os.scandir(directory) as entries:
        for entry in entries:
            if entry.is_file():
                print(f"File: {entry.path}")
            elif entry.is_dir():
                print(f"Directory: {entry.

**Pros:**  
* Avoids extra `stat` calls; each `DirEntry` already knows if it’s a file or directory.  
* Works as a context manager, ensuring the underlying iterator is closed properly.  
* Generally 2‑5× faster than `os.listdir` on directories with thousands of entries.

**Cons:**  
* Slightly more verbose than `os.listdir`.  
* Only available from Python 3.5 onward.

### Leveraging `pathlib` (Python 3.4+)

`pathlib` provides an object‑oriented, readable way to handle filesystem paths. Day to day, its `Path. And iterdir()` method mirrors `os. scandir` under the hood.

```python
from pathlib import Path

def iterate_with_pathlib(directory):
    path = Path(directory)
    for item in path.iterdir():
        if item.is_file():
            print(f"File: {item}")
        else:
            print(f"Directory: {item}")

Pros:

  • Clean, chainable syntax (Path / "subdir").
  • Methods like glob, rglob, and match simplify pattern matching.
  • Works consistently across Windows, macOS, and Linux.

Cons:

  • Slight overhead compared to raw os.scandir for extremely tight loops, but negligible for most applications.
  • Requires Python 3.4+.

Using glob and recursive glob

When you need to filter by pattern (e.Also, g. , all .But csv files), the glob module is handy. glob.glob(pattern, recursive=False) returns a list of matching paths.

import glob

def glob_csv_files(directory):
    pattern = f"{directory}/**/*.csv"
    for file_path in glob.glob(pattern, recursive=True):
        print(f"CSV file: {file_path}")

Pros:

  • Directly returns only files matching the pattern, reducing extra checks.
  • Supports **/ for recursive descent when recursive=True.

Cons:

  • Returns a list, which can be memory‑heavy for huge result sets.
  • For pure iteration without filtering, os.scandir or pathlib is more efficient.

Filtering Files During Iteration

Often you only want files of a certain type or name. Below are common filtering strategies Less friction, more output..

Extension‑Based Filter

def filter_by_extension(directory, ext):
    for entry in os.scandir(directory):
        if entry.is_file() and entry.name.lower().endswith(ext.lower()):
            print(entry.path)

Using pathlib’s match or suffix

def filter_pathlib_suffix(directory, suffix):
    for item in Path(directory).iterdir():
        if item.is_file() and item.suffix == suffix:
            print(item)

Regular Expression Filter

import re

def filter_with_regex(directory, pattern):
    regex = re.compile(pattern)
    for entry in os.Also, scandir(directory):
        if entry. is_file() and regex.search(entry.name):
            print(entry.

### Combining Multiple Criteria

You can chain conditions using `and`/`or` or build a small predicate function for readability.

```python
def is_target_file(entry):
    return (entry.is_file() and
            entry.name.startswith("report_") and
            entry.name.endswith(".xlsx"))

def filtered_scan(directory):
    for entry in os.scandir(directory):
        if is_target_file(entry):
            print(entry.path)

Recursive Traversal: Walking Subdirectories

If you're need to process files nested in subfolders, recursion is required. Python offers several built‑in ways.

os.walk

The classic generator yields a 3‑tuple (root, dirs, files) for each directory it visits.

import os

def walk_with_os_walk(directory):
    for root, dirs, files in os.walk(directory):
        for name in files:
            full_path = os.Plus, path. Day to day, join(root, name)
            print(f"File: {full_path}")
        # Optional: modify dirs in‑place to prune unwanted subdirectories
        # dirs[:] = [d for d in dirs if not d. startswith('.

### `pathlib.Path.rglob`

For a more modern approach, `pathlib` provides `rglob`, which recursively yields all matching paths.

```python
from pathlib import Path

def rglob_example(directory, pattern="*"):
    for path in Path(directory).rglob(pattern):
        if path.is_file():
            print(path)

os.scandir with Manual Recursion

While os.scandir doesn't natively support recursion, you can implement it manually for fine-grained control over traversal logic.

import os

def scan_recursive(directory):
    with os.is_dir(follow_symlinks=False):
                scan_recursive(entry.path)
            elif entry.scandir(directory) as entries:
        for entry in entries:
            if entry.is_file():
                print(entry.

## Performance Considerations

Choosing the right method depends on your use case:

- **For simple iteration**: `os.scandir` is fastest because it avoids unnecessary system calls.
- **For pattern matching**: `glob.glob` or `pathlib.Path.glob` offer convenience but may be slower for large directories.
- **For recursive traversal**: `os.walk` is efficient and widely used, while `pathlib.Path.rglob` provides a cleaner API at a slight performance cost.

Always consider memory usage when dealing with large datasets. scandir`) are preferable over lists (`glob.Generators (`os.walk`, `os.glob`) for streaming results.

## Conclusion

Efficiently iterating through directories in Python requires understanding the trade-offs between different tools. Now, scandir` for speed and low overhead, `glob` for pattern-based searches, and `pathlib` for object-oriented path manipulation. Use `os.On top of that, for recursive operations, `os. That said, rglob` offers a more intuitive interface. Path.Practically speaking, walk` remains a reliable choice, while `pathlib. By selecting the appropriate method based on your specific requirements—whether it's performance, filtering, or recursion—you can write cleaner and more maintainable file processing code.

Easier said than done, but still worth knowing.
Just Came Out

Newly Live

Neighboring Topics

Hand-Picked Neighbors

Thank you for reading about Iterate Through Files In Directory Python. We hope the information has been useful. Feel free to contact us if you have any questions. See you next time — don't forget to bookmark!
⌂ Back to Home