Python [Errno 2] 没有那个文件或目录

Python [Errno 2] No such file or directory

我正在尝试遍历仅包含 xls 文件的文件夹并逐一打开它们。注意:所有的 xsl 文件都是像“001_text.xls”,...“030_text.xls”.

这样枚举的

我的代码是:

xls_path=r'C:\path\to\my\folder'

for file in os.listdir(xls_path):
    book = xlrd.open_workbook(file)
    sheet = book.sheet_by_index(0)

    filt_xls = [ el for el in sheet.col_values(0)]

    print file.title()
    print filt_xls

问题是我只得到第一个文件 (001_text.xls) 的输出,然后是错误:

IOError: [Errno 2] No such file or directory: '002_Testo.xls'

有办法解决吗?

您可能忘记为每个文件路径添加目录名

import os.path

for file in os.listdir(xls_path):
    file = os.path.join(xls_path, file)
    .....