467,227 Members | 1,452 Online
Bytes | Developer Community
Ask Question

Home New Posts Topics Members FAQ

Post your question to a community of 467,227 developers. It's quick & easy.

Re: Why is try...except in my code not working (gzip/text files) ?

En Wed, 19 Nov 2008 13:25:03 -0200, Barak, Ron <Ro*******@lsi.com>
I need to read a file that is either a gzip or a text file (on both *nix
and Windows).
Since I didn't find a way to determine a file type, I thought of using
the following:

import gzip

FILE = "../dpm/save_state-ssp8400-F0023209_080723-110131/top.1"

file = gzip.GzipFile(FILE, "r")
except IOError:
file = open(FILE, "r")

print file.read()
Strangely, when FILE is a gzip file, all is fine.
But, when FILE is a text file (as in the above code), I get the

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

Can you explain why the try...except in my code does not work ?
Or, back to my original problem: how do I deal with a file whether it's
a text file or a gzip file ?
Note *where* the exception is raised. Until something is actually read, no
check is made for the file format.

Gabriel Genellina

Nov 20 '08 #1
  • viewed: 1100

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by bmgz | last post: by
3 posts views Thread by rohisingh@gmail.com | last post: by
15 posts views Thread by Matt Kruse | last post: by
10 posts views Thread by Xah Lee | last post: by
2 posts views Thread by comp.lang.php | last post: by
232 posts views Thread by robert maas, see http://tinyurl.com/uh3t | last post: by
4 posts views Thread by shapper | last post: by
reply views Thread by Adict | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.