<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML><HEAD><TITLE></TITLE>
<META http-equiv=Content-Type content="text/html; charset=us-ascii">
<META content="MSHTML 6.00.2900.3429" name=GENERATOR></HEAD>
<BODY><!-- Converted from text/plain format -->
<P><FONT face=Arial>Hi Guys,<BR><BR>I cannot see any difference between read1()
and read2() below, and yet, one is okay, the other give an exception.<BR><BR>In
the first run, read2() is executed, and as expected, the text file is
printed</FONT></P>
<P><FONT face=Arial><FONT face="Courier New">$ <STRONG>cat
./gzip_try.py<BR></STRONG>import gzip<BR><BR>FILE = "text_file.txt"<BR><BR>def
read2():<BR>
try:<BR> fl = gzip.GzipFile(FILE,
"r")<BR> print
fl.read()<BR> except
IOError:<BR> fl = open(FILE,
"r")<BR> print fl.read()<BR><BR>def
read1():<BR>
try:<BR> fl = gzip.GzipFile(FILE,
"r")<BR> except
IOError:<BR> fl = open(FILE,
"r")<BR><BR> print
fl.read()<BR><BR>read2()<BR>#read1()<BR><BR>$ <STRONG>python
./gzip_try.py<BR></STRONG>abc<BR>123<BR><BR></FONT></FONT><FONT face=Arial>In
the second run, read1() is executed, and an </FONT><FONT
face="Courier New">"IOError: Not a gzipped file" <FONT face=Arial>exception is
thrown from the</FONT> "print fl.read()" </FONT><FONT face=Arial>line of
read1().<BR>This is baffling to me, as the try...except should have established
that the file is a text and not gzip file !</FONT></P>
<P><FONT face=Arial><FONT face="Courier New">$ cat ./gzip_try.py<BR>import
gzip<BR><BR>FILE = "text_file.txt"<BR><BR>def read2():<BR>
try:<BR> fl = gzip.GzipFile(FILE,
"r")<BR> print
fl.read()<BR> except
IOError:<BR> fl = open(FILE,
"r")<BR> print fl.read()<BR><BR>def
read1():<BR>
try:<BR> fl = gzip.GzipFile(FILE,
"r")<BR> except
IOError:<BR> fl = open(FILE,
"r")<BR><BR> print
fl.read()<BR><BR>#read2()<BR>read1()<BR><BR>$ <STRONG>python
./gzip_try.py<BR></STRONG>Traceback (most recent call last):<BR> File
"./gzip_try.py", line 22, in <module><BR>
read1()<BR> File "./gzip_try.py", line 19, in read1<BR>
print fl.read()<BR> File "c:\Python25\lib\gzip.py", line 220, in
read<BR> self._read(readsize)<BR> File
"c:\Python25\lib\gzip.py", line 263, in _read<BR>
self._read_gzip_header()<BR> File "c:\Python25\lib\gzip.py", line 164, in
_read_gzip_header<BR> raise IOError, 'Not a gzipped
file'<BR>IOError: Not a gzipped file<BR><BR>$<BR></FONT><BR>Can anyone explain
why read1() throws an exception, while read2() behaves
correctly ?<BR><BR>Thanks,<BR>Ron. </FONT></P></BODY></HTML>