473,414 Members | 2,030 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,414 software developers and data experts.

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>
escribió:
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 =
"../dpm/save_state-ssp8400-F0023209_080723-110131/var/log/sac.log.0.gz"

try:
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
following:

$ 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
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 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
0 1270

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: bmgz | last post by:
I am having problems trying to use the gzip module, I do the followig >>>import gzip >>>file = gzip.GzipFile("testfile.txt") >>>file.write() -which params does this accept?, archive name? I...
3
by: rohisingh | last post by:
I have a tar file. The content of the file are as following. rohits@sandman 12-08-04 $ tar tvf 20041208.tar drwxr-xr-x root/root 0 2004-12-08 21:39:19 20041208/ -rw-r--r-- root/root ...
15
by: Matt Kruse | last post by:
I am far from a PHP expert, and I've been struggling to create a function which will take a javascript .js file and "compact" it as much as possible. Meaning, remove all comments and unnecessary...
10
by: Xah Lee | last post by:
today i need to use Python to decompress gzip files. since i'm familiar with Python doc and have 10 years of computing experience with 4 years in unix admin and perl, i have quickly located the...
2
by: comp.lang.php | last post by:
I am simply trying to zip together selected files into a single ZIP file. Windows XP doesn't have a native process by which you can do this, so I borrowed gzip, downloaded and installed, and upon...
14
by: Dixie | last post by:
I am trying to write some code that when I exit my application kills all of the text files in a certain folder. It is possible that some of those files are locked because someone else is using...
232
by: robert maas, see http://tinyurl.com/uh3t | last post by:
I'm working on examples of programming in several languages, all (except PHP) running under CGI so that I can show both the source files and the actually running of the examples online. The first...
4
by: shapper | last post by:
Hello, Can I use a JS file which was compressed with GZip in my Asp.Net project? What should I do to be able to use it? Thanks, Miguel
0
by: Barak, Ron | last post by:
Thanks Gabriel, Okay, I get it: I was under the impression that the format check would be done on the open. Bye, Ron. -----Original Message----- From: Gabriel Genellina Sent: Thursday,...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.