[Tutor] gzip file close

Lloyd Kvam pythonTutor at venix.com
Tue Aug 10 21:29:34 CEST 2004


Reading through the traceback, the open lines you show don't appear. 
The error gets triggered when you do a readline at line 36.  Obviously,
that's later in the processing than you would like.  The easiest way out
may be something like:
	if inputfile.endswith('.gz'):

It looks like a try block would include a large chunk of code.  If you
did stick with a try block it would be imperative to have a specific
exception (except IOError:) rather than a blank except clause.

On Tue, 2004-08-10 at 15:02, David Rock wrote:
> 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.
-- 

Lloyd Kvam
Venix Corp.
1 Court Street, Suite 378
Lebanon, NH 03766-1358

voice:	603-653-8139
fax:	801-459-9582



More information about the Tutor mailing list