简体   繁体   English

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

[英]Python XLRD no such file or directory

I have a script working where I can open files if I pass the file name but now the number of files is increasing and it doesn't make sense to have to run the script on each file individually. 我有一个脚本可以工作,如果我传递文件名,则可以在其中打开文件,但是现在文件的数量在增加,不必单独在每个文件上运行脚本都没有意义。 So I decided to let python read al files in a directory. 因此,我决定让python读取目录中的所有文件。

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"

The output from this is: 输出是:

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'

The file is clearly being read, the name of the file is output in the command line just before I get the error. 显然正在读取文件,在我得到错误之前,在命令行中输出了文件名。 I feel like this is something simple that I should be catching but it has evaded me for the last 30 minutes... 我觉得这很简单,我应该抓住它,但是在过去的30分钟里,它使我避免了...

It was a simple fix, I was being stupid. 这很简单,我当时很愚蠢。

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)

And its done 它完成了

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

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