繁体   English   中英

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)
    .....

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM