how to close a gzip.GzipFile?

Justin Guerin jguerin at cso.atmel.com
Fri Apr 1 15:26:01 EST 2005


On Friday 01 April 2005 11:54, Justin Guerin wrote:
> Hello list,
>
> gzip documentation states that calling the .close() method on a GzipFile
> doesn't really close it.  If I'm really through with it, what's the best
> way to close it?  I'm using Python2.2 (but the gzip module doesn't seem
> to be any different from 2.4).
>
> Here's my code snippet, if it's relevant:
>
> oldfileobj= file(oldfile, 'r')
darn copy and paste, the above line should be 
oldfileobj= gzip.GzipFile(oldfile, 'rb')
> oldmd5 = md5.new()
> tellold = 0
> tellnew = 1
> while tellold != tellnew:
>     line = oldfileobj.readline()
>     tellold = tellnew
>     tellnew = oldfileobj.tell()
>     oldmd5.update(line)
>
>
> At this point, I'm finished reading the file, and would like to properly
> close it.  Should I just del oldfileobj?
>
> Also, while I'm asking, does anyone know a better way to iterate through
> a gzipped file?  The gzip module doesn't support iteration, nor
> xreadlines. I need to avoid reading the entire file contents into memory,
> so I can't use "for line in oldfileobj.readlines()"
>
> Thanks for the pointers.
>
> Justin Guerin



More information about the Python-list mailing list