How to List the Files in a Folder in Python with Path.iterdir()
To work through the files in a folder, a program first needs their names. With pathlib, Path.iterdir() gives you every entry in a folder as a path you can use straight away. This guide lists a small folder of photos on Python 3.15.2, in a fixed order.
Syntax
Path("<folder>").iterdir()
| Part | What it means |
|---|---|
Path("<folder>") | A path to the folder to list, relative to where the program runs or absolute |
.iterdir() | Gives one path for each file and folder directly inside it, one at a time |
The pathlib documentation says: "When the path points to a directory, yield path objects of the directory contents", and "The children are yielded in arbitrary order, and the special entries '.' and '..' are not included."
The Example
A photos folder holds three files and a subfolder called old. The script lists it in alphabetical order.
Prerequisites
- uv and Python 3. This guide uses uv 0.13.10 with Python 3.15.2, in a project made with
uv init.
Step 1: Create the Folder
In the project folder, make photos with a subfolder and four empty files:
- macOS / Linux
- Windows (PowerShell)
mkdir -p photos/old
touch photos/daisy.jpg photos/rose.jpg photos/notes.txt photos/old/tulip.jpg
New-Item -ItemType Directory -Force photos/old
New-Item photos/daisy.jpg, photos/rose.jpg, photos/notes.txt, photos/old/tulip.jpg
mkdir -p creates photos and photos/old in one go, and touch creates empty files; neither prints anything when it works. In PowerShell, New-Item does both jobs and lists each item it creates.
Step 2: Write the Script
from pathlib import Path
folder = Path("photos")
for item in sorted(folder.iterdir()):
print(item)

iterdir() gives the entries in whatever order the file system stores them, which can differ from one computer to the next. sorted() puts them in alphabetical order, so the output is the same everywhere.
Step 3: Run It
uv run python list_folder.py
photos/daisy.jpg
photos/notes.txt
photos/old
photos/rose.jpg

Each entry is a full path that starts with the folder's name, ready to open or check. The subfolder old is listed, but not what's inside it: iterdir() looks only at one level. The command works unchanged in PowerShell. On Windows, Python writes the paths with backslashes, such as photos\daisy.jpg.
Common Mistakes
| Mistake | What happens | Do this instead |
|---|---|---|
| A folder name with a typo, or a folder that isn't there | FileNotFoundError: [Errno 2] No such file or directory: 'photo' | Check the name, or check folder.is_dir() first |
| Expecting the same order on every computer | iterdir() promises no order | Wrap it in sorted() |
| Expecting files from subfolders | Only the folder's own entries come back, so tulip.jpg in old wasn't listed | Use Path("photos").rglob("*"), which goes through every level: sorted, it also listed photos/old/tulip.jpg |
FAQ
How do I list only the files?
Keep the entries for which is_file() is true:
from pathlib import Path
files = [item for item in sorted(Path("photos").iterdir()) if item.is_file()]
print(files)
[PosixPath('photos/daisy.jpg'), PosixPath('photos/notes.txt'), PosixPath('photos/rose.jpg')]
The folder old is gone from the list. This output is from macOS, where each path prints as a PosixPath.
Does iterdir() read the files?
No. It only lists their paths. Opening and reading them is up to your code.
Conclusion
Path("photos").iterdir() listed the folder's three files and its subfolder, and sorted() put them in the same alphabetical order on every run. For files only, filter with is_file().
Additional Resources
- Path.iterdir() — listing a folder.
- Path.rglob() — every file at every level.
- pathlib — the whole module.
