如何从文件夹中读取Excel文件而不指定Excel名称?

2024-05-19 22:46:33 发布

您现在位置:Python中文网/ 问答频道 /正文

我想从文件夹中读取\u excel并加载到数据库中,但是excel每周都会刷新,并且更改文件夹中的名称(ReportWK01,ReportWK02,…)(要加载的名称)只是我需要的一个excel。你知道吗

我试图指定路径,然后阅读excel,但我不知道正确的语法。你知道吗

path = rb'\\csd-file\dd\bb\ss\uu\To_Load'
results = os.path.join(path, rb"*\*.xlsx")
df = pd.read_excel(results, engine='python')

这是给我写信

ValueError: Must explicitly set engine if not passing in buffer or path for io.

Tags: path路径文件夹名称数据库语法excelresults
1条回答
网友
1楼 · 发布于 2024-05-19 22:46:33
## can you try reading it based on most recent time stamp
import os                                                                   
import glob             

folder_path ='\\csd-file\dd\bb\ss\uu\To_Load'

# glob.glob returns all paths matching the pattern.
excel_files = list(glob.glob(os.path.join(folder_path, '*.xls*')))

mod_dates = [os.path.getmtime(f) for f in excel_files]
print(mod_dates)
# sort by mod_dates.
file_date = sorted(zip(excel_files, mod_dates),reverse=True)
print("*"*100)
print(file_date)
newest_file_path = file_date[0][0]
df = pd.read_excel(newest_file_path)

相关问题 更多 >