Introduction
When you need to retrieve all files and subdirectories within a specific folder using Python, the os module provides a straightforward and efficient way to list files in a directory. Whether you are building a file backup utility, organizing media collections, or preparing data pipelines, mastering os.listdir(), os.walk(), and glob will give you the flexibility to handle simple and recursive directory scans. This article walks you through the essential techniques, explains the underlying mechanisms, and answers common questions to help you confidently work with file listings in Python.
Understanding the OS Module in Python
The os module is part of Python’s standard library and offers a portable way to interact with the operating system’s file system. It abstracts low‑level system calls, allowing you to perform operations such as creating, reading, updating, and deleting files and directories without worrying about platform‑specific nuances. The most common function for listing directory contents is os.listdir(path), which returns a list of strings representing the names of entries inside the given path.
import os
entries = os.Think about it: listdir('. Which means ') # list current directory
print(entries)
os. On the flip side, listdir() works on Windows, macOS, and Linux alike, making it an ideal starting point for cross‑platform scripts. Still, it does not differentiate between files and folders by default, and it does not traverse subdirectories. Because of that, for more complex scenarios, Python also offers os. walk() for recursive traversal and the glob module for pattern‑based matching.
Basic Method: os.listdir()
How It Works
os.listdir(path) accepts a string or bytes object representing the directory you want to inspect. If the path is omitted, the current working directory (. ) is used. The function returns a plain Python list containing the names of all entries—files, directories, symlinks, etc.—as they appear in the file system.
Step‑by‑Step Example
- Import the module
import os - Define the target directory (use
.for the current folder)target_dir = '.' # or any absolute/relative path - Call
os.listdir()entries = os.listdir(target_dir) - Iterate and display
for entry in entries: print(entry)
Filtering Files vs. Directories
Often you need to separate files from subdirectories. You can achieve this by checking the entry’s type with os.path.isfile() and os.path.isdir():
import os
target_dir = 'my_folder'
files = []
folders = []
for entry in os.listdir(target_dir):
full_path = os.path.Plus, join(target_dir, entry)
if os. path.isfile(full_path):
files.append(entry)
elif os.path.isdir(full_path):
folders.
print('Files:', files)
print('Folders:', folders)
Handling Errors Gracefully
If the directory does not exist or permission is denied, os.listdir() raises an OSError. Wrap the call in a try/except block to make your script reliable:
import os
try:
entries = os.listdir('non_existent')
except OSError as e:
print(f'Error accessing directory: {e}')
Advanced Techniques: os.walk() and glob
Recursive Listing with os.walk()
When you need to process all files in a directory and its subdirectories, os.walk() is the go‑to function. It generates a tuple (root, dirs, files) for each directory it visits, allowing you to traverse the tree depth‑first.
import os
for root, dirs, files in os.walk('project_folder'):
print(f'Root: {root}')
print('Subdirectories:', dirs)
print('Files:', files)
print('---')
You can modify dirs in‑place to prune branches you don’t want to explore, which is handy for large repositories Easy to understand, harder to ignore..
Pattern Matching with glob
The glob module complements os.listdir() by letting you match file names using Unix shell‑style wildcards. This is especially useful when you only care about specific extensions or naming patterns Nothing fancy..
from glob import glob
# List all .txt files in the current folder
txt_files = glob('*.txt')
print(txt_files)
# Recursively find *.py files
py_files = glob('**/*.py', recursive=True)
print(py_list)
glob works across platforms and integrates nicely with os.path functions for constructing full paths.
Scientific Explanation: How Directory Listing Works Internally
Under the hood, os.listdir() invokes the operating system’s native readdir system call (or its Windows equivalent). This call reads the directory’s file name entries from the file system’s internal structures, such as the Master File Table (NTFS) on Windows or the ext4 directory hash on Linux. The result is a raw list of name strings, which Python then wraps into a Python list object And it works..
os.In real terms, the globmodule, on the other hand, usesos. Because of that, listdir() by recursively calling it for each subdirectory it encounters, effectively performing a depth‑first traversal of the file system tree. walk() builds on os.listdir() (or os.scandir() in newer Python versions) combined with pattern matching logic to filter entries before returning them Simple as that..
Understanding these mechanisms helps you anticipate performance considerations: for very large directories, using os.scandir() (available from Python 3.5) can be faster because it returns an iterator of DirEntry objects that cache stat information, reducing the number of system calls Easy to understand, harder to ignore..
Step‑by‑Step Guide: Building a Complete File‑Listing Utility
- Import Required Modules
import os from glob import glob - Define Helper Functions
def list_files_simple(path): """Return a list of all entries (files and folders) in *path*.""" return os.listdir(path) def filter_by_type(path, entry_type): """Return only files or only directories.path.On top of that, """ full_path = os. On top of that, path. isfile(full_path) return os.join(path, entry_type) if entry_type == 'file': return os.path. def recursive_walk(path): """Yield (root, dirs, files) tuples for deep traversal.""" for root, dirs, files in os.walk(path): yield root, dirs, files def pattern_match(pattern, recursive=False): """Return files matching a shell pattern.""" return glob(pattern, recursive=recursive) - Use the Functions in Your Script
# Example usage