Python XLRD没有这样的文件或目录

时间:2014-01-29 18:49:31

标签: python xlrd

我有一个脚本工作,如果我传递文件名,我可以打开文件,但现在文件的数量正在增加,并且必须单独在每个文件上运行脚本是没有意义的。所以我决定让python在目录中读取al文件。

for root, dirs, files in os.walk("Approved_LRPMP_Worksheets/"):
    for fyle in files:
        if fyle.endswith(".xlsx"):
            print fyle
            book = xlrd.open_workbook(fyle)
            print "book opened"

这个输出是:

I found a file: Agoura Hills LRPMP Review Sheet.xlsx
Traceback (most recent call last):
  File "test.py", line 21, in <module>
    book = xlrd.open_workbook(fyle)
File "/Library/Python/2.7/site-packages/xlrd/__init__.py", line 394, in open_workbook
  f = open(filename, "rb")
IOError: [Errno 2] No such file or directory: 'Agoura Hills LRPMP Review Sheet.xlsx'

显然正在读取文件,在我收到错误之前,在命令行中输出文件的名称。我觉得这很简单,我应该抓住,但它在过去的30分钟内已经回避了我......

1 个答案:

答案 0 :(得分:1)

这是一个简单的修复,我是愚蠢的。

for root, dirs, files in os.walk("Approved_LRPMP_Worksheets/"):
    for fyle in files:
        if fyle.endswith(".xlsx"):
            print "I found a file: " + fyle
            fyleName = "Approved_LRPMP_Worksheets/"+fyle
            book = xlrd.open_workbook(fyleName)

完成了