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')
用顷让dir函数汪乎拦加上循环即可。比如将D:\ABC文件夹内的所有文件显示到A列,代码如下:
Sub 遍历所有文件()Dim Fn$, N%
Fn = Dir("D:\ABC\*.*")
While Fn <> ""
困胡 N = N + 1
Range("A" & N) = Fn
Fn = Dir
Wend
End Sub
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)