Question
How can I list all files in a directory in Python and store them in a list?
For example, I want to read the contents of a folder, get the file names, and add those file names to a Python list for later use.
Short Answer
By the end of this page, you will understand how to read directory contents in Python, how to collect file names into a list, and how to choose between os.listdir(), os.scandir(), and pathlib. You will also learn how to filter out directories when you only want files, and how this pattern is used in real programs.
Concept
In Python, listing files in a directory means asking the operating system for the contents of a folder. The result can include both files and subdirectories, so an important part of the task is deciding exactly what you want:
- all entries in the folder
- only files
- only directories
- full paths instead of just names
This matters because many real programs work with file systems:
- batch-processing images
- reading CSV files from a data folder
- loading configuration files
- scanning logs
- building upload tools
Python provides multiple ways to do this:
os.listdir(path)returns names in a directoryos.scandir(path)returns entry objects with helpful methods like.is_file()pathlib.Path(path).iterdir()gives a more modern, object-oriented approach
If your goal is specifically to get all files and store them in a list, you usually need two steps:
- read the directory contents
- filter out anything that is not a file
The main idea is simple: a directory contains entries, and your code chooses which of those entries belong in the final list.
Mental Model
Think of a directory like a box of labeled items.
- Some items are files
- Some items are folders inside the folder
When Python opens the box, it sees a list of entries. Your job is to decide:
- Do I want every item?
- Do I want only papers, not smaller boxes?
- Do I want just the names, or the full location of each item?
So listing files is like looking inside a storage box and writing down only the items that match your rule.
Syntax and Examples
Basic approach with os.listdir()
import os
folder_path = "my_folder"
files = os.listdir(folder_path)
print(files)
This returns a list of names inside the directory. Important detail: it may include both files and subdirectories.
Example output:
['notes.txt', 'image.png', 'archive', 'data.csv']
Here, archive might be a subdirectory, not a file.
Get only files with os.listdir()
import os
folder_path = "my_folder"
files = [
name for name in os.listdir(folder_path)
if os.path.isfile(os.path.join(folder_path, name))
]
print(files)
Explanation
os.listdir(folder_path)gets all names in the folder.os.path.join(folder_path, name)builds the full path.os.path.isfile(...)checks whether that entry is a file.
Step by Step Execution
Consider this code:
import os
folder_path = "my_folder"
files = []
for name in os.listdir(folder_path):
full_path = os.path.join(folder_path, name)
if os.path.isfile(full_path):
files.append(name)
print(files)
Assume my_folder contains:
report.txtphoto.jpgbackup(a directory)
Step by step
os.listdir(folder_path)returns something like:['report.txt', 'photo.jpg', 'backup']- The loop starts with
name = 'report.txt'. os.path.join(folder_path, name)creates:'my_folder/report.txt'os.path.isfile(full_path)returnsTrue, so is added to .
Real World Use Cases
1. Processing uploaded files
A web app may store uploaded files in a folder and then process each one.
import os
uploads = [
name for name in os.listdir("uploads")
if os.path.isfile(os.path.join("uploads", name))
]
2. Reading all CSV files in a data directory
from pathlib import Path
csv_files = [file for file in Path("data").iterdir() if file.is_file() and file.suffix == ".csv"]
3. Cleaning up temporary files
Scripts often scan a temp folder and delete old files.
4. Loading templates or configuration files
Programs may read a directory at startup and load all matching files.
5. Media batch jobs
Image converters, video tools, and backup scripts often start by listing files in a folder.
Real Codebase Usage
In real projects, developers usually combine directory listing with other patterns.
Guard clauses
Check that the directory exists before reading it:
from pathlib import Path
folder = Path("data")
if not folder.exists():
print("Directory does not exist")
else:
files = [item for item in folder.iterdir() if item.is_file()]
Validation
Developers often filter by extension:
from pathlib import Path
images = [
item for item in Path("images").iterdir()
if item.is_file() and item.suffix.lower() in {".png", ".jpg", ".jpeg"}
]
Early returns in functions
from pathlib import Path
def get_text_files(folder_name):
folder = Path(folder_name)
if folder.exists() folder.is_dir():
[]
[file.name file folder.iterdir() file.is_file() file.suffix == ]
Common Mistakes
Mistake 1: Assuming os.listdir() returns only files
Broken example:
import os
files = os.listdir("my_folder")
print(files)
Problem:
- This includes subdirectories too.
Fix:
import os
files = [
name for name in os.listdir("my_folder")
if os.path.isfile(os.path.join("my_folder", name))
]
Mistake 2: Using isfile(name) without the full path
Broken example:
import os
folder = "my_folder"
files = [name for name in os.listdir(folder) if os.path.isfile(name)]
Problem:
nameis just a file name, not the full path.- Python checks in the current working directory, not necessarily in
my_folder.
Fix:
Comparisons
| Approach | Returns | Best for | Notes |
|---|---|---|---|
os.listdir(path) | List of names as strings | Simple cases | Includes files and directories |
os.scandir(path) | Directory entry objects | Efficient file checks | Good when you need .is_file() or .is_dir() |
Path(path).iterdir() | Path objects | Modern readable code | Works well with other pathlib features |
os.listdir() vs os.scandir()
Cheat Sheet
# All entries in a directory
import os
os.listdir("my_folder")
# Only files using os.listdir
import os
files = [
name for name in os.listdir("my_folder")
if os.path.isfile(os.path.join("my_folder", name))
]
# Only files using os.scandir
import os
files = [entry.name for entry in os.scandir("my_folder") if entry.is_file()]
# Only files using pathlib
from pathlib import Path
files = [item.name for item in Path("my_folder").iterdir() if item.is_file()]
# Full paths with pathlib
from pathlib import Path
files = [str(item) for item in Path("my_folder").iterdir() if item.is_file()]
# Sorted results
files = sorted(item.name for item in Path("my_folder").iterdir() if item.is_file())
Rules to remember
os.listdir()includes directories too.
FAQ
How do I list only files in a directory in Python?
Use a filter with os.path.isfile() or Path.is_file().
from pathlib import Path
files = [item.name for item in Path("my_folder").iterdir() if item.is_file()]
Does os.listdir() include subdirectories?
Yes. It returns all entries in the directory, including files and subdirectories.
How do I get full file paths instead of just file names?
Use pathlib or join the folder path manually.
from pathlib import Path
files = [str(item) for item in Path("my_folder").iterdir() if item.is_file()]
What is the difference between os.listdir() and os.scandir()?
os.listdir() returns names. os.scandir() returns entry objects that can directly tell you whether an item is a file or directory.
Mini Project
Description
Build a small Python utility that scans a folder and collects all text files into a list. This demonstrates how to read a directory, filter for files, and work with full paths in a practical way. This kind of script is useful for log processing, document collection, or simple batch jobs.
Goal
Create a function that returns all .txt files from a given directory as a sorted list.
Requirements
- Accept a folder path as input.
- Return only files, not subdirectories.
- Keep only files with the
.txtextension. - Return the results in sorted order.
- If the folder does not exist, return an empty list.
Keep learning
Related questions
@staticmethod vs @classmethod in Python Explained
Learn the difference between @staticmethod and @classmethod in Python with clear examples, use cases, mistakes, and a mini project.
Catch Multiple Exceptions in One except Block in Python
Learn how to catch multiple exceptions in one Python except block using tuples, with examples, mistakes, and real-world usage.
Convert Bytes to String in Python 3
Learn how to convert bytes to str in Python 3 using decode(), text mode, and proper encodings with practical examples.