How to read the content of all files in a folder using Python?
To read the contents of all files in a folder, you can use the os module to get all the files in the folder, then use the open() function to open each file and read its contents.
Here is an example 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)
# 打开文件并读取内容
with open(file_path, 'r') as file:
file_content = file.read()
# 输出文件内容
print(file_content)
Please replace folder_path with the path of the folder you want to read. This will iterate through each file in the folder, open and read its contents, and then output the content to the console.