473,668 Members | 2,522 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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(F ILE, "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\li b\gzip.py", line 220, in read
self._read(read size)
File "c:\Python25\li b\gzip.py", line 263, in _read
self._read_gzip _header()
File "c:\Python25\li b\gzip.py", line 164, in _read_gzip_head er
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 1283

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

Similar topics

3
2667
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 get this ERROR: Traceback (most recent call last): File "<stdin>", line 1, in ?
3
4574
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 1576 2004-12-08 21:39:19 20041208/README drwxr-xr-x root/root 0 2004-12-08 21:27:31 20041208/snapshot_01/ -rw-r--r-- was/was 103010606 2004-12-08 16:37:38 20041208/snapshot_01/tpv-2004 1208-1350.xml.gz
15
2328
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 whitespace. "Obfuscation" is not necessary. Obviously, the compacted javascript should run identically to the original javascript. Can anyone point me to an existing function that does this? Thanks!
10
3905
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 official doc: http://python.org/doc/2.4.1/lib/module-gzip.html but after a minute of scanning, please someone tell me what the fuck is it talking about?
2
1853
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 using it: <? $msg = exec('gzip -q --suffix .zip '. @join(' ', $fileArray)); ?> All of my files named in $fileArray were horribly mangled!! They all were converted individually into files with .zip extension that lost
14
2615
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 them, in which case they will have the first two letters of their name replaced with ~$. How can I write a routine that will delete all the normal *.txt files, but not attempt to delete the ~$ ones, which brings up an error message and prevents the...
232
13242
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 set of examples, after decoding the HTML FORM contents, merely verifies the text within a field to make sure it is a valid representation of an integer, without any junk thrown in, i.e. it must satisfy the regular expression: ^ *?+ *$ If the...
4
4427
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
1959
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, November 20, 2008 02:06 To: python-list@python.org Subject: Re: Why is try...except in my code not working (gzip/text files) ?
0
8459
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8889
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8790
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8572
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8652
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7391
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
4372
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2017
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1779
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.