【问题标题】:Python [Errno 2] No such file or directoryPython [Errno 2] 没有这样的文件或目录
【发布时间】:2017-01-29 12:03:54
【问题描述】:

我正在尝试遍历仅包含 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'

有办法解决吗?

【问题讨论】:

  • 文件夹应该只包含 xls 文件。带有任何扩展名的文件002_Testo 都可能导致错误,因为-我猜-xlrd.open_workbook(file).xls 扩展名添加到file
  • 有这样的文件吗?

标签: python excel python-2.7 path xls


【解决方案1】:

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

import os.path

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

【讨论】:

    猜你喜欢
    • 2015-11-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-12-13
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多