In file manipulation scripts, you often need to process all files inside a specific folder. There are two main ways to get a list of contents in a directory in Python:
- pathlib module (Recommended): An object-oriented, modern approach introduced in Python 3.4.
- os module: A set of basic functions that has been used for a long time.
This article explains how to use these two methods and provides advanced techniques for distinguishing between files and directories.
1. Using the pathlib Module (Recommended)
Currently, the pathlib module is the standard way to handle file paths in Python. It treats paths as objects, allowing you to write intuitive and readable code.
The iterdir() Method
To get the contents of a directory, use the iterdir() method of the Path object. This returns an iterator, so you can loop through it with a for loop or convert it to a list using list().
from pathlib import Path
# Target directory path (specifying current directory ".")
target_dir = Path(".")
print(f"--- Contents of '{target_dir.resolve()}' ---")
# Scan contents with iterdir()
for item in target_dir.iterdir():
print(item.name)
Execution Result (Example):
--- /Users/username/projects/myapp Contents ---
main.py
requirements.txt
assets
README.md
Getting as a List
If you want to keep the contents as a list, list comprehensions make it concise.
# Create a list of path objects
file_list = [p for p in target_dir.iterdir()]
# Create a list of filenames (strings) only
filename_list = [p.name for p in target_dir.iterdir()]
print(filename_list)
Execution Result (Example):
['main.py', 'requirements.txt', 'assets', 'README.md']
2. Using os.listdir() (Traditional Method)
Using the os.listdir() function from the os module allows you to get a list of entry names as strings.
import os
# Target directory path
target_path = "data_folder"
# Check if directory exists before executing
if os.path.exists(target_path):
# Get a list of file and directory names
items = os.listdir(target_path)
print(items)
else:
print(f"Directory '{target_path}' not found.")
Execution Result (Example):
['data_01.csv', 'data_02.csv', 'archive']
This is simple, but since the return value is just a “string,” you need to use separate os.path functions to join paths or check attributes. Therefore, using pathlib is recommended in recent years.
Advanced: Distinguishing Between Files and Directories
Sometimes files and directories (folders) are mixed in the list you retrieve. If you want to process them separately, pathlib makes this very smooth.
from pathlib import Path
# Directory to investigate
base_path = Path("project_root")
print("=== File List ===")
# Check with is_file()
files = [p.name for p in base_path.iterdir() if p.is_file()]
print(files)
print("\n=== Directory List ===")
# Check with is_dir()
directories = [p.name for p in base_path.iterdir() if p.is_dir()]
print(directories)
Execution Result (Example):
=== File List ===
['config.json', 'script.py']
=== Directory List ===
['logs', 'templates', 'static']
Summary
- pathlib.Path(path).iterdir(): The recommended method. You can treat paths as objects, making checks like
is_fileeasy. - os.listdir(path): The traditional method. Returns a list of filename strings.
Unless you have a specific reason not to, I recommend using pathlib for its excellent readability and functionality.
