title: Python xlrd.Book: how to close the files?
body: <p>I read 150 excel files in a loop, opening them with <code>xlrd.open_workbook()</code>, which returns a <code>Book</code> object. At the end, when I tried to <code>umount</code> the volume, I was unable, and when I checked with <code>lsof</code>, I found that 6 of the files were still open:</p>
  
  <pre><code>$ lsof | grep volumename
  
  python2   32349         deeenes  mem       REG               0,40    138240     181517 /.../150119.xls
  python2   32349         deeenes  mem       REG               0,40    135168     181482 /.../150609.xls
  python2   32349         deeenes  mem       REG               0,40    140800     181495 /.../140828.xls
  python2   32349         deeenes    5r      REG               0,40    140800     181495 /.../140828.xls
  python2   32349         deeenes    6r      REG               0,40    135168     181482 /.../150609.xls
  python2   32349         deeenes    7r      REG               0,40    138240     181517 /.../150119.xls
  </code></pre>
  
  <p>Here is my function I read the xls files with:
  <em>(stripped for clarity)</em></p>
  
  <pre><code>import sys
  import xlrd
  from xlrd.biffh import XLRDError
  
  def read_xls(xls_file, sheet = '', return_table = True):
      try:
          book = xlrd.open_workbook(xls_file, on_demand = True)
          try:
              sheet = book.sheet_by_name(sheet)
          except XLRDError:
              sheet = book.sheet_by_index(0)
          table = [[str(c.value) for c in sheet.row(i)] for i in xrange(sheet.nrows)]
          if not return_table:
              table = None
          return table
      except IOError:
          sys.stdout.write('No such file: %s\n' % xls_file)
      sys.stdout.flush()
  </code></pre>
  
  <p>The <code>Book</code> object does not have <code>close()</code> method, neither have any open file type objects among its properties, except the stdout. This <a href="http://www.lexicon.net/sjmachin/xlrd.html#xlrd.Book-class" rel="noreferrer">howto</a> does not tell about this (haven't found the official docs). I don't see how I could close the file, and also it is weird that 6 remains open after reading 150 of them.</p>
  
  <p><em>Edit:</em> it might be related to <a href="https://pythonhosted.org/xlrd3/general.html#loading-worksheets-on-demand" rel="noreferrer">this</a>, but still should not leave open files, and I don't want to read all sheets.</p>