As a professional Linux coder and full-stack developer, I often need to traverse file systems to locate files and directories. The os.listdir() function in Python provides a convenient way to get a list of files and subfolders in a given path.
In this comprehensive guide, I‘ll cover everything you need to know about using os.listdir() in Python. We‘ll start with the basics, then dive into practical examples, edge cases, and alternatives. By the end, you‘ll have a deep understanding of this useful file system tool.
What is os.listdir()?
The os.listdir() method returns a list containing the names of the files and subfolders in the specified path. Here is the basic syntax:
import os
list_of_files = os.listdir(path)
The path parameter specifies which folder to get the contents of. If you don‘t specify a path, it will use the current working directory by default.
Some key notes about os.listdir():
- The returned list contains strings representing filenames. Not full file paths.
- Files and folders are not guaranteed to be sorted.
- Hidden files on Linux and macOS will be included.
Now let‘s look at some examples of using os.listdir() in real code.
Listing Files in a Directory
Here is how to use os.listdir() to get all files and subfolders from a folder path:
import os
files = os.listdir(‘/home/user/documents‘)
for file in files:
print(file)
This will print out each file and folder name on its own line.
Keep in mind this only returns the names as strings, not full paths. To get absolute paths, you would append the path like:
full_path = os.path.join(‘/home/user/documents‘, file)
You can also filter the results to only return files or only folders. Here‘s how to get only files:
import os
all_files = os.listdir(‘/home/user/documents‘)
files = [f for f in all_files if os.path.isfile(os.path.join(‘/home/user/documents‘, f))]
for file in files:
print(file)
And to filter for only folders:
folders = [f for f in all_files if os.path.isdir(os.path.join(‘/home/user/documents‘, f))]
As you can see, os.listdir() combined with other path methods like os.path.isfile() or os.path.join() enable easy file wrangling.
List Files in Current Working Directory
If you want to list files from the current directory without specifying a path, simply call os.listdir() without parameters:
import os
files = os.listdir()
print(files)
To make this more readable, I suggest using os.getcwd() to get the current working directory explicitly:
cwd = os.getcwd()
files = os.listdir(cwd)
print(files)
This avoids confusion by being clear you want the contents of the current folder.
Recursively List Files
os.listdir() only returns top level files and folders. To recursively list all contents including subfolder contents, use os.walk() instead:
import os
for root, dirs, files in os.walk(‘/home/user/documents‘):
for file in files:
print(os.path.join(root, file))
The os.walk() method will traverse the entire tree, allowing you to access subfolders and files.
So in cases when you need a deep recursive listing, os.walk() is usually a better choice than calling os.listdir() repeatedly.
List Files on macOS/Linux vs Windows
One small caveat of os.listdir() is that it will include hidden files and folders (.filename) on Unix-like systems.
For example, listing files on macOS or Linux would include .config folders:
config.txt
.config
myfile.js
While on Windows, hidden files and folders won‘t be included:
config.txt
myfile.js
So keep this in mind if porting code between OSes. You may want to explicitly filter on filenames starting with .
Additionally, the path separator will be / on macOS/Linux vs \ on Windows. Use os.path.join() when constructing paths for cross-platform support.
Catching Errors
It‘s good practice to wrap os.listdir() in a try/except in case the path doesn‘t exist:
import os
path = ‘/invalid/path‘
try:
files = os.listdir(path)
except FileNotFoundError:
print(‘Path does not exist‘)
This will print the custom error message instead of a scary stack trace.
For even more precision, you can catch specific error cases like permission issues:
import os
try:
files = os.listdir(path)
except FileNotFoundError:
print(‘Path does not exist‘)
except PermissionError:
print(‘No permission to read path‘)
Alternative Functions
While os.listdir() is the preferred method for getting directory listings in Python, there are a few alternatives:
- os.scandir() – Returns directory info objects instead of only names
- pathlib – Object oriented alternative to os.path
- glob – Returns files matching a specified pattern
Here is a quick example of each:
os.scandir()
import os
with os.scandir(‘/home/user/documents‘) as dir_contents:
for entry in dir_contents:
print(entry.name)
pathlib
from pathlib import Path
files = [f for f in Path(‘/home/user/documents‘).iterdir() if f.is_file()]
for file in files:
print(file.name)
glob
import glob
list_of_files = glob.glob(‘/home/user/documents/*.txt‘)
print(list_of_files)
My recommendation is to stick with the basic os.listdir() approach in most cases for simplicity. But these alternatives can be useful in certain situations.
When Not to Use os.listdir()
While os.listdir() is handy for getting a quick directory listing, there are a few cases where it should be avoided:
- Huge directories – Listing hundreds of thousands of files will be slow and memory intensive. Use a generator like
os.scandir()to iterate through batches instead. - Permission issues – Any issues trying to access the files will fail the whole operation. Check permissions first or handle errors.
- External devices – Providing a path to a USB drive or network share that gets disconnected could result in crashes. Check it exists first.
- Concurrency – Multiple processes/threads reading the same directory could run into timing issues resulting in failures or inconsistent results.
So keep these constraints in mind as your application scales in size and complexity.
Conclusion
After reading this guide, you should have a detailed understanding of using Python‘s os.listdir() function to traverse and read file directories.
We covered the basics of listing folder contents, filtering files from folders, traversing recursively, cross-platform usage, error handling, and alternatives.
While a simple function, mastering os.listdir() usage unlocks the capability to easily obtain directory listings for all your Python file processing tasks.
Let me know in the comments if you have any other questions! I‘m happy to provide code samples or debugging advice as a professional developer. File I/O is a critical skill for any programmer or data scientist working with Python.


