How to List all Files/Folders/Directories in Python
If you need to list all Files, Folders, or Directories in Python code, then you can use the listdirs method from the os package.
import os
print(os.listdir("/path/to/directory/")
This returns a flat list of everything in the directory — both files and folders, without any path prefix. I use this for quick scripts that need to process all files in a folder, like batch renaming or log parsing.
If you need to filter by file type, combine it with os.path.isfile() or os.path.isdir(). For recursive listing (including subdirectories), use os.walk() instead. On Python 3.5+, pathlib.Path.iterdir() is a more modern alternative that returns Path objects you can chain methods on.