Python / Excel - IOError:[Errno 2]没有这样的文件或目录:

时间:2014-09-29 21:39:31

标签: python excel xlrd xlsxwriter

尝试从文件中提取.xlsx文档并将数据编译成单个工作表。

尽管文件存在,但仍接收IOError

计划如下

#-------------- loop that pulls in files from folder--------------
import os

#create directory from which to pull the files
rootdir = r'C:\Users\username\Desktop\Mults'

for subdir, dir, files in os.walk(rootdir):
for file in files:
    print os.path.join(subdir,file)
#----------------------merge work books-----------------------

import xlrd
import xlsxwriter


wb = xlsxwriter.Workbook('merged.xls')
ws = wb.add_worksheet()
for file in files:
    r = xlrd.open_workbook(file)
    head, tail = os.path.split(file)
    count = 0
    for sheet in r:
        if sheet.number_of_rows()>0:
            count += 1
    for sheet in r:
        if sheet.number_of_rosw()>0:
            if count == 1:
                sheet_name = tail
            else:
                sheet_name = "%s_%s" (tail, sheet.name)
            new_sheet = wb.create_sheet(sheet_name)
            new_sheet.write_reader(sheet)
            new_sheet.close()
wb.close()

返回错误如下

doc1.xlsx
doc2.xlsx
doc3.xlsx
doc4.xlsx

Traceback (most recent call last):
  File "C:\Users\username\Desktop\Work\Python\excel practice\xlsx - loops files - 09204.py", line 23, in <module>
    r = xlrd.open_workbook(file)
  File "C:\Python27\lib\site-packages\xlrd\__init__.py", line 394, in open_workbook
    f = open(filename, "rb")
IOError: [Errno 2] No such file or directory: 'doc1.xlsx'

有任何建议或更改吗?

另外,如果我朝着正确的方向前进,有什么建议吗?

我是蟒蛇世界的新手,所以任何建议都会非常感激!

谢谢!

2 个答案:

答案 0 :(得分:2)

您正在打开没有路径的纯文件名;你忽略了目录组件。

不要打印os.path.join()结果,实际使用它:

filename = os.path.join(subdir, file) 
r = xlrd.open_workbook(filename)

答案 1 :(得分:0)

对于第一个问题......

而不是:

r = xlrd.open_workbook(file)

使用:

r = xlrd.open_workbook(os.path.join(subdir,file))

对于TypeError: 而不是:

for sheet in r:
    if sheet.number_of_rows()>0:
        count += 1

使用:

for nsheet in r.sheet_names() #you need a list of sheet names to loop throug
    sheet = r.sheet_by_name(nsheet) #then you create a sheet object with each name in the list
    if sheet.nrows>0: #use the property nrows of the sheet object to count the number of rows
        count += 1

对第二个for循环执行相同的操作。