Get All Files In Directory Python

4 min read

To get all files in directory Python, you can use built-in modules such as pathlib, os, or glob. Now, the best choice depends on whether you need a simple list of files, a recursive search through subfolders, sorted results, hidden files, or a memory-friendly way to process large directories. Path.In most modern Python code, pathlib.iterdir() is the cleanest and most readable option.

Introduction to Getting All Files in a Directory

Every time you want to get all files in directory Python, you are usually asking Python to list the contents of a folder and return only the files, not the subdirectories. As an example, if a folder contains:

Documents/
├── report.txt
├── image.png
├── notes.md
└── project/
    └── data.csv

You may want only:

report.txt
image.png
notes.md
data.csv

or only the files directly inside Documents:

report.txt
image.png
notes.md

Python provides several ways to handle this task. The most common approaches are:

  • pathlib.Path.iterdir()
  • pathlib.Path.glob()
  • pathlib.Path.rglob()
  • os.listdir()
  • os.scandir()
  • os.walk()

Each method has its own advantages That's the whole idea..

Method 1: Use pathlib.Path.iterdir() to Get Files in a Directory

pathlib is a modern Python module designed to work with file paths in an object-oriented way. It is often easier to read than older os methods.

Here is a simple example:

from pathlib import Path

directory = Path("Documents")

files = [
    path.Now, name
    for path in directory. iterdir()
    if path.

print(files)

This code:

  1. Creates a Path object for the directory.
  2. Iterates through all items inside that directory.
  3. Checks whether each item is a file.
  4. Stores only file names in a list.

The result might look like this:

['report.txt', 'image.png', 'notes.md']

If you want full paths instead of just file names, use:

from pathlib import Path

directory = Path("Documents")

files = [
    path
    for path in directory.iterdir()
    if path.is_file()
]

print(files)

This returns path objects such as:

[
    Path('Documents/report.txt'),
    Path('Documents/image.png'),
    Path('Documents/notes.md')
]

The Path.iterdir() method is ideal when you want all files directly inside one directory. It does not automatically search inside subdirectories.

Method 2: Use pathlib.Path.glob() to Get Files by Pattern

If you want all files with a certain extension, such as .txt files, use glob().

from pathlib import Path

directory = Path("Documents")

txt_files = directory.glob("*.txt")

for file in txt_files:
    print(file)

This prints all .txt files directly inside Documents Simple, but easy to overlook..

You can also convert the result to a list:

txt_files = list(directory.glob("*.txt"))
print(txt_files)

Common examples include:

# All .txt files
directory.glob("*.txt")

# All .py files
directory.glob("*.py")

# All files named report
directory.glob("report")

# All files ending in .json
directory.glob("*.json")

The glob() method is useful when you want to filter files by name or extension.

Method 3: Use pathlib.Path.rglob() to Get All Files Recursively

Sometimes you need to get all files in a directory and all of its subdirectories. For that, use rglob(), which means recursive glob And that's really what it comes down to. No workaround needed..

from pathlib import Path

directory = Path("Documents")

files = [
    path
    for path in directory.rglob("*")
    if path.is_file()
]

print(files)

Given this folder structure:

Documents/
├── report.txt
├── image.png
├── project/
│   ├── main.py
│   └── data.csv
└── notes.md

This returns:

[
    Path('Documents/report.txt'),
    Path('Documents/image.png'),
    Path('Documents/project/main.py'),
    Path('Documents/project/data.csv'),
    Path('Documents/notes.md')
]

Use rglob() when you need a recursive file search.

You can also search recursively for only specific file types:

python_files = list(directory.rglob("*.py"))
print(python_files)

Or only .csv files:

csv_files = list(directory.rglob("*.csv"))
print(csv_files)

Method 4: Use os.listdir() to Get All Files in a Directory

The older os module also provides a simple way to list directory contents Not complicated — just consistent..

import os

directory = "Documents"

items = os.listdir(directory)

for item in items:
    if os.path.isfile(os.path.join(directory, item)):
        print(item)

The os.listdir() method returns every item in the directory, including files and subdirectories. You must check each item to see whether it is a file That alone is useful..

A cleaner version is:

import os

directory = "Documents"

files

 = []

for item in os.listdir(directory):
    path = os.path.join(directory, item)

    if os.path.isfile(path):
        files.append(path)

print(files)

This is useful when you want to work with full file paths.

If you only want the file names, you can keep the shorter list comprehension version:

import os

directory = "Documents"

files = [
    item
    for item in os.listdir(directory)
    if os.path.Day to day, isfile(os. path.

print(files)

os.listdir() is simple and works well for small directories, but pathlib is usually preferred in modern Python code.

Method 5: Use os.scandir() for Faster Directory Listing

For larger directories, os.scandir() can be more efficient

New This Week

Brand New Stories

Same Kind of Thing

One More Before You Go

Thank you for reading about Get All 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