Hi, I'm having a problem with exception handling....this is the error message that I get with my code:
raise ReadError("not a bzip2 file")
ReadError: not a bzip2 file
so to handle it I'm trying this: -
try: bzip.decompress(test_list)
-
except: ReadError
-
continue
-
but that doesn't work. I looked through the list of Standard Exceptions but didn't see 'ReadError' on the list. What should I be using?
11 1739
You need to define what type of Error you're catching before the colon: -
try:
-
bzip.decompress(test_list)
-
except ReadError:
-
continue
-
and your indentation was off... but I didn't check to see if that caused a syntax error... however you would know if it did.
Thanks for the reply. No, I wasn't getting a syntax error but I'm thinking that I may have things out of place in my code. I tried your suggestion but I'm still getting the error message. This code is to take a 'broken' png file and fix it. There should be 4 '\r\n' characters in it and when they are in the right place the file is fixed. I'm opening the file and replacing all of the '\r\n' with '\n' and then have a loop to go through and replace '\n' positions 1,2,3,4 with '\r\n' and then check the file. If there's an error, it should continue to check the next positions 1,2,3,5, then check it, etc..... -
import bzip
-
f = open("corrupted[1].png.bz2","rb")
-
s = f.read()
-
test_str = s.replace("\r\n","\n")
-
test_list=s.split("\n")
-
## subtract one from stop because the last letter is a "\n"
-
stop = len(test_list) - 1
-
for j in range( 1, stop-3 ):
-
for k in range( j+1, stop-2 ):
-
for m in range( k+1, stop-1 ):
-
for n in range(m+1, stop ):
-
numbers_list = [ j, k, m, n]
-
ctr=0
-
final_str=""
-
while ctr < stop:
-
final_str += test_list[ctr]
-
if ctr in numbers_list:
-
final_str += "\r\n"
-
else:
-
final_str += "\n"
-
ctr += 1
-
repr(final_str)
-
try:
-
bzip.decompress(test_list)
-
except ReadError:
-
continue
-
print j, k, m, n
-
What is the error message that you are getting?
bvdet 2,851
Recognized Expert Moderator Specialist
Thekid,
It looks like you are trying to decompress a list. That should result in a TypeError every time.
What is the error message that you are getting?
This is the complete error message:
Traceback (most recent call last):
File "C:\Python25\pngfix.py", line 1, in <module>
import bzip
File "C:\Python25\bzip.py", line 2, in <module>
tar = tarfile.open('corrupted[1].png.bz2', 'r:bz2')
File "C:\Python25\lib\tarfile.py", line 1035, in open
return func(name, filemode, fileobj)
File "C:\Python25\lib\tarfile.py", line 1129, in bz2open
raise ReadError("not a bzip2 file")
ReadError: not a bzip2 file
Bvdget: yes, I basically need to decompress a list but I tried changing it to 'TypeError' but I still get the above message. I'm thinking something in my code isn't in the right place but I'm not sure what.
I think that what bvdet was trying to say is that you cannot use bzip to decompress a python list. It has to be the file itself.
I think that what bvdet was trying to say is that you cannot use bzip to decompress a python list. It has to be the file itself.
Ok, but I have a png file, not just a list. It has become corrupt during FTP transfer:
"This is because both ASCII and binary files can be sent in binary mode with no problems, but sending a binary file in ASCII mode will corrupt the binary file's structure."
So I have the png file "corrupted[1].png.bz2" which won't open correctly because some of the '\r\n' in the png have been changed during transfer. I have to fix it. I'm trying to run a loop through the file to replace them. Once the right ones have been replaced, the file should be able to open. So....I'm trying to open the file, replace certain positions, then check the file to see if it's correct.
I should probably clarify that it's a bzip file which won't decompress until the cr/lf are corrected in the png, then it will successfully open.
You still seem to be missing what we're saying...
You're feeding the list to the decompress function... when you should be feeding the file itself. If you're trying to "fix" the file, then the modifications that you're making need to be written to disk (ie, open('somefile.bz2', 'wb') ) before you attempt to decompress it.
Hope you understand what I'm saying...
You still seem to be missing what we're saying...
You're feeding the list to the decompress function... when you should be feeding the file itself. If you're trying to "fix" the file, then the modifications that you're making need to be written to disk (ie, open('somefile.bz2', 'wb') ) before you attempt to decompress it.
Hope you understand what I'm saying...
I think I understand, I'm opening the file as a list and changing it but then I'm trying to decompress that changed list instead of saving the changes to the file and decompressing it.
I'm trying to decompress that changed list instead of saving the changes to the file and decompressing it.
Yes, and the problem is that bzip is expecting a file handle and not a python list object. So write the list to a temporary file somewhere before you decompress it with bzip, if that doesn't work reopen the file w/ 'wb' and the contents will be cleared so you can write your next list (your next attempt at fixing the file contents), then retry to decompress it. The simple fact is that you cannot decompress a list no matter how bad you want to because it's a python object and not a pointer to a file, which is what bzip is expecting.
Sign in to post your reply or Sign up for a free account.
Similar topics |
by: adi |
last post by:
Dear all,
This is more like a theoretical or conceptual question:
which is better, using exception or return code for
a .NET component?
I had created a COM object (using VB6), which uses...
|
by: Daniel Wilson |
last post by:
I am having exception-handling and stability problems with .NET. I will
have a block of managed code inside try...catch and will still get a generic
..NET exception box that will tell me which...
|
by: Noor |
last post by:
please tell the technique of centralize exception handling without try catch
blocks in c#.
|
by: Master of C++ |
last post by:
Hi,
I am an absolute newbie to Exception Handling, and I am trying to
retrofit exception handling to a LOT of C++ code that I've written
earlier. I am just looking for a bare-bones, low-tech...
|
by: tom |
last post by:
Hi,
I am developing a WinForm application and I am looking for a guide on where
to place Exception Handling. My application is designed into three tiers
UI, Business Objects, and Data Access...
| |
by: C# Learner |
last post by:
Some time ago, I remember reading a discussion about the strengths and
weaknesses of exception handling. One of the weaknesses that was put
forward was that exception handling is inefficient (in...
|
by: craig |
last post by:
I am wondering if there are some best practices for determining a strategy
for using try/catch blocks within an application.
My current thoughts are:
1. The code the initiates any high-level...
|
by: Ele |
last post by:
When Exception handling disabled compiler still spits out "C++ exception
handler used." Why is that?
Why does it ask for "Specify /EHsc"?
Thanks!
c:\Program Files\Microsoft Visual Studio...
|
by: Zytan |
last post by:
Ok something simple like int.Parse(string) can throw these exceptions:
ArgumentNullException, FormatException, OverflowException
I don't want my program to just crash on an exception, so I must...
|
by: George2 |
last post by:
Hello everyone,
Such code segment is used to check whether function call or exception-
handling mechanism runs out of memory first (written by Bjarne),
void perverted()
{
try{
|
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,...
| |
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: 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...
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
|
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...
| |