Skip to main content

How to List the Files in a Folder in Python with Path.iterdir()

· 4 min read
Jagdish Kumawat
Founder @ Dewiride

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​

Syntax
Path("<folder>").iterdir()
PartWhat 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:

Terminal
mkdir -p photos/old
touch 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​

list_folder.py
from pathlib import Path

folder = Path("photos")
for item in sorted(folder.iterdir()):
print(item)

The list_folder.py script in a macOS-style code window: it imports Path, sets folder to Path("photos"), and prints each item of sorted(folder.iterdir()).

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​

Terminal
uv run python list_folder.py
Output
photos/daisy.jpg
photos/notes.txt
photos/old
photos/rose.jpg

The terminal after uv run python list_folder.py: photos/daisy.jpg, photos/notes.txt, photos/old and 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​

MistakeWhat happensDo this instead
A folder name with a typo, or a folder that isn't thereFileNotFoundError: [Errno 2] No such file or directory: 'photo'Check the name, or check folder.is_dir() first
Expecting the same order on every computeriterdir() promises no orderWrap it in sorted()
Expecting files from subfoldersOnly the folder's own entries come back, so tulip.jpg in old wasn't listedUse 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:

files.py
from pathlib import Path

files = [item for item in sorted(Path("photos").iterdir()) if item.is_file()]
print(files)
Output
[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​