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 0 1270 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
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...
|
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 ...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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
|
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,...
|
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...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
|
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,...
|
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...
|
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...
|
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...
|
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,...
|
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...
| |