Two functionaly identical functions -> different results ??!

Barak, Ron Ron.Barak at lsi.com
Thu Nov 20 02:24:01 EST 2008


Hi Guys,

I cannot see any difference between read1() and read2() below, and yet, one is okay, the other give an exception.

In the first run, read2() is executed, and as expected, the text file is printed

$ cat ./gzip_try.py
import gzip

FILE = "text_file.txt"

def read2():
    try:
        fl = gzip.GzipFile(FILE, "r")
        print fl.read()
    except IOError:
        fl = open(FILE, "r")
        print fl.read()

def read1():
    try:
        fl = gzip.GzipFile(FILE, "r")
    except IOError:
        fl = open(FILE, "r")

    print fl.read()

read2()
#read1()

$ python ./gzip_try.py
abc
123

In the second run, read1() is executed, and an "IOError: Not a gzipped file" exception is thrown from the "print fl.read()" line of read1().
This is baffling to me, as the try...except should have established that the file is a text and not gzip file !

$ cat ./gzip_try.py
import gzip

FILE = "text_file.txt"

def read2():
    try:
        fl = gzip.GzipFile(FILE, "r")
        print fl.read()
    except IOError:
        fl = open(FILE, "r")
        print fl.read()

def read1():
    try:
        fl = gzip.GzipFile(FILE, "r")
    except IOError:
        fl = open(FILE, "r")

    print fl.read()

#read2()
read1()

$ python ./gzip_try.py
Traceback (most recent call last):
  File "./gzip_try.py", line 22, in <module>
    read1()
  File "./gzip_try.py", line 19, in read1
    print fl.read()
  File "c:\Python25\lib\gzip.py", line 220, in read
    self._read(readsize)
  File "c:\Python25\lib\gzip.py", line 263, in _read
    self._read_gzip_header()
  File "c:\Python25\lib\gzip.py", line 164, in _read_gzip_header
    raise IOError, 'Not a gzipped file'
IOError: Not a gzipped file

$

Can anyone explain why read1() throws an exception, while read2() behaves correctly ?

Thanks,
Ron.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20081120/56723538/attachment.html>


More information about the Python-list mailing list