Python List Files in Folder: Complete Guide

One way to get all files in a folder is by using the os module. The specific steps are as follows:

import os

# 指定文件夹路径
folder_path = 'path_to_your_folder'

# 获取文件夹下所有文件的文件名
file_list = os.listdir(folder_path)

# 打印所有文件名
for file_name in file_list:
    print(file_name)

This way, you can get the names of all files in the specified folder. If you want to get the absolute path of a file, you can use the os.path.join() method to combine the folder path and the file name.

bannerAds