How to close workbook

4,424 views
Skip to first unread message

Cc

unread,
Mar 16, 2008, 6:37:16 AM3/16/08
to python-excel
Hi,

We can open a workbook in xlrd, like this:
book = xlrd.open_workbook(filename)
However, after reading all the necessary data, how can we close the
workbook to free the related file handle and other resources?

Searched thru the xlrd source code, but can not find anything related
to close().

Thanks.





Chris Withers

unread,
Mar 16, 2008, 5:30:43 PM3/16/08
to python...@googlegroups.com

Why do you want to close it? Are you using Windows or something
similarly silly? ;-)

del book

...will probably free any resources you're worried about.

cheers,

Chris

--
Simplistix - Content Management, Zope & Python Consulting
- http://www.simplistix.co.uk

John Machin

unread,
Mar 16, 2008, 5:58:57 PM3/16/08
to python-excel
Significant resources used by open_workbook:

(1) a file object used to access the physical file -- Book.__init__
cleans this up as soon as it's obtained (2); look for "close" and
"del"

(2) the str object or mmap object containing the file contents --
Book.__init__ cleans this up as soon as it's obtained (3) ... unless
(3) *is* (2)

(3) Book.mem which is the str object or mmap object containing the
contiguous data stream to be parsed -- this is released by
open_workbook calling Book.release_resources just before open_workbook
returns to its caller

(4) the actual data which is released implicitly (and thus available
for garbage collection) when you stop referring to it

This style of operating should not cause a problem, unless the
operating system is not returning freed memory to its pool:

for i in xrange(10000):
wb =xlrd.open_workbook('foo%d.xls' % i)
do_something_with(wb)
# following is optional
del wb
do_some_other_work_with(something_else)

This will blow up, because references to the Book object are being
retained unnecessarily:

wbs = [xlrd.open_workbook('foo%d.xls' % i) for i in xrange(10000)]
for wb in wbs:
do_something_with(wb)

What average and max sizes (in Mb) are your XLS files, and how many do
you open in one thread or process? Are you actually experiencing a
problem?

Cheers,
John

Cc

unread,
Mar 17, 2008, 4:14:30 AM3/17/08
to python-excel

Thank you for the detailed descriptions.

My case is hundreds of XLS files to deal with(in a big loop), each in
100K bytes.
Having not met with any problem yet, just feel somewhat nervous about
the resource
occupation.

Now I know before returning from open_workbook(), xlrd already tried
the best to
release resources, while whole file content loaded into memory and
referenced by
WorkBook object/hierarchy. So In my code, just forget the XLS file
handle and mmap
object, just remember to:
del sheet
del workbook
when necessary.

Perhaps my wrong impression comes from the function name of
open_workbook().
As a former C programmer, I have the habbit to close() anything open()-
ed, especially
related to file. If the function name is load_workbook(), or there is
some section
about the resource management pattern in the document -- just put
there your excellent
explanations, my bad habbit should not have been triggered :)))

Good work. Thank you for the great package!

Regards,

Cc

On Mar 17, 5:58 am, John Machin <sjmac...@lexicon.net> wrote:
-snip-
Reply all
Reply to author
Forward
0 new messages