2014-01-29 35 views
1

我有一個腳本工作,我可以打開文件,如果我通過文件名,但現在文件數量增加,並沒有意義必須在每個文件上運行腳本個別。所以我決定讓python讀取目錄中的al文件。Python XLRD沒有這樣的文件或目錄

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

這是一個簡單的修復,我是愚蠢的。

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) 

而且其完成

相關問題