473,486 Members | 2,136 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Exception handling

Thekid
145 New Member
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:

Expand|Select|Wrap|Line Numbers
  1. try: bzip.decompress(test_list)
  2.                except: ReadError
  3.                continue
  4.  
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?
Mar 25 '08 #1
11 1739
jlm699
314 Contributor
You need to define what type of Error you're catching before the colon:
Expand|Select|Wrap|Line Numbers
  1. try:
  2.     bzip.decompress(test_list)
  3. except ReadError:
  4.     continue
  5.  
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.
Mar 25 '08 #2
Thekid
145 New Member
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.....

Expand|Select|Wrap|Line Numbers
  1. import bzip
  2. f = open("corrupted[1].png.bz2","rb")
  3. s = f.read()
  4. test_str = s.replace("\r\n","\n")
  5. test_list=s.split("\n")
  6. ##   subtract one from stop because the last letter is a "\n"
  7. stop = len(test_list) - 1
  8. for j in range( 1, stop-3 ):
  9.    for k in range( j+1, stop-2 ):
  10.       for m in range( k+1, stop-1 ):
  11.          for n in range(m+1, stop ):
  12.             numbers_list = [ j, k, m, n]
  13.             ctr=0
  14.             final_str=""
  15.             while ctr < stop:
  16.                final_str += test_list[ctr]
  17.                if ctr in numbers_list:     
  18.                   final_str += "\r\n"
  19.                else:
  20.                   final_str += "\n"        
  21.                ctr += 1
  22.                repr(final_str)
  23.                try:
  24.                   bzip.decompress(test_list)
  25.                except ReadError:
  26.                   continue
  27.             print j, k, m, n
  28.  
Mar 25 '08 #3
jlm699
314 Contributor
What is the error message that you are getting?
Mar 26 '08 #4
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.
Mar 26 '08 #5
Thekid
145 New Member
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.
Mar 26 '08 #6
jlm699
314 Contributor
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.
Mar 26 '08 #7
Thekid
145 New Member
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.
Mar 26 '08 #8
Thekid
145 New Member
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.
Mar 26 '08 #9
jlm699
314 Contributor
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...
Mar 26 '08 #10
Thekid
145 New Member
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.
Mar 27 '08 #11
jlm699
314 Contributor
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.
Mar 27 '08 #12

Sign in to post your reply or Sign up for a free account.

Similar topics

11
2828
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...
6
2319
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...
7
5961
by: Noor | last post by:
please tell the technique of centralize exception handling without try catch blocks in c#.
3
2734
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...
2
2577
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...
9
2520
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...
44
4155
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...
4
5108
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...
41
3020
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...
1
3086
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{
0
7100
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,...
0
6964
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
7126
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
7175
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
7330
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
5434
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,...
1
4865
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...
1
598
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
262
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...

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.