By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,908 Members | 1,945 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,908 IT Pros & Developers. It's quick & easy.

Exception handling

Thekid
100+
P: 145
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
Share this Question
Share on Google+
11 Replies


jlm699
100+
P: 314
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
100+
P: 145
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
100+
P: 314
What is the error message that you are getting?
Mar 26 '08 #4

bvdet
Expert Mod 2.5K+
P: 2,851
Thekid,

It looks like you are trying to decompress a list. That should result in a TypeError every time.
Mar 26 '08 #5

Thekid
100+
P: 145
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
100+
P: 314
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
100+
P: 145
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
100+
P: 145
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
100+
P: 314
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
100+
P: 145
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
100+
P: 314
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

Post your reply

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