Question

How to List All Files in a Directory in Python

pythondirectory

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 directory
  • os.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:

  1. read the directory contents
  2. 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.

Take Quiz

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.
  • The list comprehension collects only file names.

Using os.scandir()

import os

folder_path = "my_folder"
files = [entry.name for entry in os.scandir(folder_path) if entry.is_file()]

print(files)

This is often clearer and can be more efficient because each entry already knows whether it is a file.

Using pathlib

from pathlib import Path

folder_path = Path("my_folder")
files = [item.name for item in folder_path.iterdir() if item.is_file()]

print(files)

pathlib is a modern and readable way to work with paths in Python.

Get full file paths instead of names

from pathlib import Path

folder_path = Path("my_folder")
files = [str(item) for item in folder_path.iterdir() if item.is_file()]

print(files)

This is useful when you need to open the files later.

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.txt
  • photo.jpg
  • backup (a directory)

Step by step

  1. os.listdir(folder_path) returns something like:
    ['report.txt', 'photo.jpg', 'backup']
    
  2. The loop starts with name = 'report.txt'.
  3. os.path.join(folder_path, name) creates:
    'my_folder/report.txt'
    
  4. os.path.isfile(full_path) returns True, so 'report.txt' is added to files.
  5. Next, name = 'photo.jpg'.
  6. Its full path is checked and is also a file, so it is added.
  7. Next, name = 'backup'.
  8. os.path.isfile('my_folder/backup') returns False because it is a directory.
  9. It is skipped.
  10. Final result:
    ['report.txt', 'photo.jpg']
    

This shows the key pattern: list entries, test each one, keep only files.

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.

Take Quiz

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 not folder.exists() or not folder.is_dir():
        return []

    return [file.name for file in folder.iterdir() if file.is_file() and file.suffix == ".txt"]

Error handling

Accessing a directory can fail because of permissions or missing paths.

import os

try:
    files = os.listdir("private_folder")
except FileNotFoundError:
    files = []
except PermissionError:
    files = []

Path objects in larger codebases

Many modern Python projects prefer pathlib because path operations become easier to read and maintain.

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:

  • name is just a file name, not the full path.
  • Python checks in the current working directory, not necessarily in my_folder.

Fix:

import os

folder = "my_folder"
files = [name for name in os.listdir(folder) if os.path.isfile(os.path.join(folder, name))]

Mistake 3: Forgetting that order is not guaranteed

Do not rely on a specific order unless you sort the results.

from pathlib import Path

files = sorted(item.name for item in Path("my_folder").iterdir() if item.is_file())

Mistake 4: Ignoring hidden files

Some systems may include hidden files like .DS_Store or .gitignore.

If needed, filter them out:

from pathlib import Path

files = [
    item.name for item in Path("my_folder").iterdir()
    if item.is_file() and not item.name.startswith('.')
]

Mistake 5: Not handling missing directories

Broken example:

import os

files = os.listdir("missing_folder")

This raises FileNotFoundError.

Fix:

import os

try:
    files = os.listdir("missing_folder")
except FileNotFoundError:
    files = []

Comparisons

ApproachReturnsBest forNotes
os.listdir(path)List of names as stringsSimple casesIncludes files and directories
os.scandir(path)Directory entry objectsEfficient file checksGood when you need .is_file() or .is_dir()
Path(path).iterdir()Path objectsModern readable codeWorks well with other pathlib features

os.listdir() vs os.scandir()

  • Use os.listdir() when you only need names.
  • Use os.scandir() when you also need file type checks.

os vs pathlib

Featureos modulepathlib
StyleFunctionalObject-oriented
Path joiningos.path.join(a, b)Path(a) / b
ReadabilityFamiliar and commonOften easier to read
Modern Python useStill widely usedOften preferred in new code

Names vs full paths

Result typeExampleWhen to use
File name onlyreport.txtDisplaying names to users
Full pathdata/report.txtOpening, moving, or deleting files
Take Quiz

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.
  • Use os.path.join() when checking names returned by os.listdir().
  • Use item.is_file() or entry.is_file() to filter files.
  • Use sorted(...) if order matters.
  • Handle FileNotFoundError if the folder may not exist.

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.

Should I use os or pathlib in Python?

Both are correct. pathlib is often easier to read and is commonly preferred in modern Python code.

How do I list files with a specific extension?

Filter by suffix.

from pathlib import Path
text_files = [f.name for f in Path("docs").iterdir() if f.is_file() and f.suffix == ".txt"]

Why does os.path.isfile(name) sometimes fail?

Because name may not include the directory path. You often need os.path.join(folder, name) first.

Take Quiz

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

  1. Accept a folder path as input.
  2. Return only files, not subdirectories.
  3. Keep only files with the .txt extension.
  4. Return the results in sorted order.
  5. If the folder does not exist, return an empty list.
Take Quiz

Keep learning

Related questions

View more