import pandas as pd import os # 用os.walk遍历文件;用.endswith判断文件后缀 dfs = pd.DataFrame() for root, dirs, files in os.walk(r'C:\Users\ll\Desktop\pandas\excels'): for file in files: if file.endswith('.xlsx'): # 构建绝对路径 file_name = os.path.join(root, file) # print(file_name) df = pd.read_excel(file_name) # print(df) dfs = pd.concat([dfs, df]) # print(dfs) dfs.to_excel(r'C:\Users\ll\Desktop\new.xlsx')