[Tutor] Reading .gz files
Hanlie Pretorius
hanlie.pretorius at gmail.com
Fri Jul 29 12:12:00 CEST 2011
Hi,
I'm working on Windows XP with Python 2.6.
I need to read and process hundreds of binary files that are in the
.gz archive format.
I found a site (http://www.doughellmann.com/PyMOTW/gzip/) and tried
their code with two files: one of the hundreds of files that I need to
process (f1 below) and one that I created with 7-Zip from a text file
that contains the text 'Text to test gzip module.' (f2 below). The
code and the output follow:
[code]
import gzip
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'
print if2.read()
print 'done with f2'
finally:
if1.close()
if2.close()
[/code]
[output]
done with f1
Text to test gzip module.
done with f2
[/output]
This seems to indicate that something is wrong with f1 (the GSMaP file - a
binary file), but I can unzip the file manually and read it with a
python script. I have hundreds of GSMAP files that have unique
archived file names, but they all unzip to the same binary file, so I
have to process the archived files in the python script.
I would be grateful if someone could help me achieve this.
Regards
Hanlie
More information about the Tutor
mailing list