“[Errno 2]没有这样的文件或目录:'Book10.xls'”

2024-09-29 23:20:37 发布

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

我正在尝试使用jupyter笔记本合并几个excel工作簿。我将包含excel工作簿的目录命名为files,其输出为:

['Book10.xls',
 'Book13.xls',
 'Book12.xls',
 'Book16.xls',
 'Book9.xls',
 'Book15.xls',
 'Book14.xls',
 'Book8.xls',
 'Book18.xls',
 'Book4.xls',
 'Book6.xls',
 'Book3.xls',
 'Book1.xls']

我使用以下循环尝试将数据提取到python中,并将它们粘贴到更大的数据帧中:

df = pd.DataFrame()
for file in files:
     if file.endswith('.xls'):
         df = df.append(pd.read_excel(file), ignore_index=True) 
df.head()

但是,我得到了一个路径错误:

[Errno 2] No such file or directory: 'Book10.xls'

我读过一个类似的线程(Python open() gives FileNotFoundError/IOError: Errno 2 No such file or directory),其中提到使用IDE可以更改文件路径。我还试图os.listdr()os.getcwd再次检查我是否在正确的目录中(我似乎在正确的目录中)

如果您能帮助您找出文件路径未被识别的原因,并提出让循环正常工作的建议,我们将不胜感激


Tags: or数据no路径目录dffilesxls

热门问题