[Tutor] gzip file close

David Rock david at graniteweb.com
Tue Aug 10 21:02:54 CEST 2004


I am trying to use the gzip module to open files for reading data, but I
think I am having a problem with using it with non-gzipped files. What I
want to do is pass a wordlist of filenames, some gzipped, some not on
the commandline and have the program make the distiction between the
two, but I get the following results:

$parse_bpimagelist.py -m esdgp1 esdgp1.0408061210.bpimagelist.gz esdgp1.0408091210.bpimagelist 

Traceback (most recent call last):
  File "/home/drock/parse_bpimagelist.py", line 235, in ?
    d = getdata( file )
  File "/home/drock/parse_bpimagelist.py", line 36, in getdata
    buffer = fp_input.readline()
  File "/usr/lib/python2.3/gzip.py", line 379, in readline
    c = self.read(readsize)
  File "/usr/lib/python2.3/gzip.py", line 224, in read
    self._read(readsize)
  File "/usr/lib/python2.3/gzip.py", line 260, in _read
    self._read_gzip_header()
  File "/usr/lib/python2.3/gzip.py", line 161, in _read_gzip_header
    raise IOError, 'Not a gzipped file'
IOError: Not a gzipped file

The first file is gzipped, the second is not. The try block I am trying to use looks like this:
    try:
        fp_input = gzip.open( inputfile, 'rb' )
    except:
        fp_input = open( inputfile, 'rb' )

The idea was that if the file is not gzipped, it would do the second
file open instead. One thing I found in the documentation is that gzip's
close() method doesn't close the gzipped file, but I don't understand
what that means. I don't think that's the issue because even if the
non-gzipped file is listed first, it still doesn't work. I thought maybe
it was because the close() operation wasn't working properly, but I
don't see evidence to support that.

Is it possible that the exception is NOT getting handled properly?

Thanks.


-- 
David Rock
david at graniteweb.com
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 189 bytes
Desc: not available
Url : http://mail.python.org/pipermail/tutor/attachments/20040810/9d3b3f15/attachment.pgp


More information about the Tutor mailing list