How to list all files and folders in a folder using Python

We can make use of the glob module to list all files from a folder using Python.

Example:

import glob

folder_path = "/data/2023"

file_paths = glob.glob(folder_path + "/*")

for file_path in file_paths:
    print(file_path)
Output:
/data/2023/report.xlsx
/data/2023/corrections.txt
/data/2023/2023_data.csv
Display Paths Files and Folders in Python
Reference:


https://docs.python.org/3/library/glob.html

Comments & Discussion

Facing issues? Have questions? Post them here! We're happy to help!