You can use the listdir
function of the os
module to list all the files in the folder, and then use a loop to read the files one by one.
The following is a sample code:
import os folder_path = '/path/to/folder' # 列出文件夹下的所有文件 file_list = os.listdir(folder_path) # 循环读取文件 for file_name in file_list: file_path = os.path.join(folder_path, file_name) if os.path.isfile(file_path): with open(file_path, 'r') as file: # 在这里对文件进行处理 # 例如打印文件内容 print(file.read())
Please replace /path/to/folder
with the actual path of the folder you want to read.
The above is the detailed content of How to read all files in a folder in python. For more information, please follow other related articles on the PHP Chinese website!