共計 543 個字符,預計需要花費 2 分鐘才能閱讀完成。
讀取文件夾中的文件可以使用 Python 的 os 模塊和 glob 模塊。以下是兩種常用的方法:
方法一:使用 os 模塊的 listdir 函數
import os
folder_path = ' 文件夾路徑 '
for filename in os.listdir(folder_path):
file_path = os.path.join(folder_path, filename)
if os.path.isfile(file_path):
with open(file_path, 'r') as file:
# 處理文件內容
pass
方法二:使用 glob 模塊的 glob 函數
import glob
folder_path = ' 文件夾路徑 '
file_list = glob.glob(folder_path + '/*')
for file_path in file_list:
if os.path.isfile(file_path):
with open(file_path, 'r') as file:
# 處理文件內容
pass
以上代碼中,需要將’文件夾路徑’替換為實際的文件夾路徑。然后遍歷文件夾中的文件,使用 open 函數打開文件并進行處理。
丸趣 TV 網 – 提供最優質的資源集合!
正文完