[Tutor] Reading .gz files

Steven D'Aprano steve at pearwood.info
Fri Jul 29 15:41:51 CEST 2011


Oh, I forgot to say something else...

Hanlie Pretorius wrote:

> f1 = 'GSMaP_MVK+.20050101.00.0.1deg.hourly.v484.gz'
> f2 = ''text.txt.gz'
> if1 = gzip.open(f1, 'rb')
> if2 = gzip.open(f2,'rb')
> try:
>    print if1.read()
>    print 'done with f1'


Once you've read the file once, the file pointer is at the end of the 
file, and reading it again returns the empty string. Example:


 >>> y = gzip.open('spam.gz', 'rb')
 >>> y.read()  # read to the end
'spam spam spam'
 >>> y.read()  # anything left?
''
 >>> y.seek(0)  # go back to the beginning
 >>> y.read()
'spam spam spam'



-- 
Steven



More information about the Tutor mailing list