Python xlrd.Book: how to close the files?

后端 未结 1 482
有刺的猬
有刺的猬 2020-12-16 16:38

I read 150 excel files in a loop, opening them with xlrd.open_workbook(), which returns a Book object. At the end, when I tried to umount

相关标签:
1条回答
  • 2020-12-16 17:18

    In case you open a workbook with on_demand = True for a more economic resource use (see here how does it work), you need to call release_resources() method at the end. As a minimal example:

    import xlrd
    
    book = xlrd.open_workbook('workbook.xls', on_demand = True)
    sheet = book.sheet_by_index(0)
    data = [[str(c.value) for c in sheet.row(i)] for i in xrange(sheet.nrows)]
    book.release_resources()
    del book
    
    0 讨论(0)
提交回复
热议问题