【发布时间】:2021-09-27 15:39:53
【问题描述】:
我正在尝试将多个 excel 文件导入 DataFrame,但出现错误:FileNotFoundError: [Errno 2] No such file or directory: 'test1.xlsx'
代码:
path=os.getcwd()
files = os.listdir(path+"/testimport")
df = pd.DataFrame()
for f in files:
data = pd.read_excel(f,
sheet_name = "Data",
skiprows = range(0, 4),
usecols = "B:I,P:V")
df = df.append(data)
但是,当我尝试打印文件时,它确实有效:
in: for f in files:
print(f)
out: test1.xlsx
test2.xlsx
这怎么可能以及如何解决?我尝试了绝对路径,但结果相同。
【问题讨论】:
-
为
read_excel提供完整路径。listdir只返回文件名。 -
也许是
pd.read_excel(path + "/testimport/" + f,,因为看起来f只包含文件名,而不是完整路径。
标签: python pandas import-from-excel