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
-

Facing issues? Have Questions? Post them here! I am happy to answer!


Author Info:

Rakesh (He/Him) is a seasoned developer with over 10 years of experience in web and app development, and a deep knowledge of operating systems. Author of insightful How-To articles for Code2care.

Follow him on: X

You can also reach out to him via e-mail: rakesh@code2care.org

Copyright © Code2care 2024 | Privacy Policy | About Us | Contact Us | Sitemap