How to List Files in a Directory in Python
If you want to list files in a folder in Python, there are two common beginner-friendly options:
os.listdir()for a simple list of namespathlibfor cleaner path handling
This page shows how to:
- List everything inside a directory
- Show only files, not subdirectories
- Print file names or full file paths
- Use beginner-friendly methods first
Quick answer #
from pathlib import Path
folder = Path(".")
for item in folder.iterdir():
if item.is_file():
print(item.name)
This prints file names in the current directory and skips subdirectories.
What this page helps you do #
- List everything inside a directory
- Show only files, not subdirectories
- Print file names or full file paths
- Use beginner-friendly methods first
Simplest way: use os.listdir() #
The os.listdir() function returns the names of items inside a folder.
That means the result can include:
- Files
- Subdirectories
If you use ".", Python looks at the current working directory.
import os
for name in os.listdir("."):
print(name)
Example output #
notes.txt
photo.jpg
data
script.py
Here:
notes.txt,photo.jpg, andscript.pyare filesdatamight be a folder
So os.listdir() is useful when you want to see everything in a directory, but it does not automatically separate files from folders.
If you want to understand this function in more detail, see os.listdir() explained.
How to list only files #
If you only want files, you need to check each item.
A common way is to use:
os.listdir()to get namesos.path.join()to build the full pathos.path.isfile()to test whether the item is a file
import os
folder = "."
for name in os.listdir(folder):
full_path = os.path.join(folder, name)
if os.path.isfile(full_path):
print(name)
Why use os.path.join()? #
os.listdir() gives you only the item name, such as:
"notes.txt"
But os.path.isfile() usually needs the full path to check the correct item:
"./notes.txt"
That is why this line matters:
full_path = os.path.join(folder, name)
It combines the folder and file name safely.
If you are not familiar with this, see how os.path.join() works.
Beginner-friendly modern option: pathlib #
Many beginners find pathlib easier to read because it treats paths as objects.
You can create a Path object for a folder, loop through its contents, and check whether each item is a file.
from pathlib import Path
folder = Path(".")
for item in folder.iterdir():
if item.is_file():
print(item.name)
What this does #
Path(".")means the current directoryfolder.iterdir()loops through everything inside that directoryitem.is_file()keeps only filesitem.nameprints just the file name
Print full paths with pathlib #
If you want the path instead of just the name, print the Path object itself:
from pathlib import Path
folder = Path(".")
for item in folder.iterdir():
if item.is_file():
print(item)
You can also print the absolute path:
from pathlib import Path
folder = Path(".")
for item in folder.iterdir():
if item.is_file():
print(item.resolve())
item.name→ file name onlyitemorstr(item)→ relative pathitem.resolve()→ full absolute path
For a broader introduction, see working with file paths in Python.
How to include full paths #
Sometimes you need the complete file path, not just the file name.
This is common before you:
- Open a file
- Delete a file
- Check whether a file exists
- Pass the path to another function
Full paths with os #
import os
folder = "my_folder"
for name in os.listdir(folder):
full_path = os.path.join(folder, name)
if os.path.isfile(full_path):
print(full_path)
This prints paths like:
my_folder/report.txt
my_folder/data.csv
Full paths with pathlib #
from pathlib import Path
folder = Path("my_folder")
for item in folder.iterdir():
if item.is_file():
print(item)
If you need the absolute path:
from pathlib import Path
folder = Path("my_folder")
for item in folder.iterdir():
if item.is_file():
print(item.resolve())
After listing files, you may want to check whether a file exists in Python or read a file in Python.
Common problems beginners hit #
A few common mistakes can make directory listing confusing.
Using a path that does not exist #
If the folder name is wrong, Python cannot list its contents.
import os
print(os.path.exists("your_folder"))
If this prints False, the path is wrong or Python is looking in a different location than you expected.
If you get an error, see how to fix FileNotFoundError: [Errno 2] No such file or directory.
Forgetting that listdir() includes directories too #
This code prints everything:
import os
for name in os.listdir("."):
print(name)
If you expected only files, you must filter with os.path.isfile() or item.is_file().
Using only the file name when a full path is required #
This often causes problems when the file is not in the current directory.
Wrong idea:
import os
folder = "my_folder"
for name in os.listdir(folder):
if os.path.isfile(name):
print(name)
Better:
import os
folder = "my_folder"
for name in os.listdir(folder):
full_path = os.path.join(folder, name)
if os.path.isfile(full_path):
print(full_path)
Confusing the current working directory with the script location #
When you use ".", Python uses the current working directory, not always the folder where your script is saved.
This command helps you check where Python is looking:
import os
print(os.getcwd())
You can also check with pathlib:
from pathlib import Path
print(Path(".").resolve())
Permission problems #
Sometimes the folder exists, but Python is not allowed to read it. In that case, you may see a permission error.
See how to fix PermissionError: [Errno 13] Permission denied.
FAQ #
How do I list only files and not folders in Python? #
Filter each item with os.path.isfile() or use pathlib and check item.is_file().
What is the easiest way to list files in a directory? #
For beginners, pathlib is often the clearest option. os.listdir() is also common and simple.
Why does my code show folders too? #
Functions like os.listdir() return all items in the directory, including subdirectories.
How do I get full file paths instead of names? #
Use os.path.join(folder, name) with os, or print the Path object in pathlib.
Why can Python not find the folder I gave it? #
You may be using a relative path and your current working directory is different from what you expected.