Login or Sign up Help | Site Map
Connecting Tech Pros Worldwide

Exception handling

Question posted by: Thekid (Member) on March 25th, 2008 08:33 PM
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:

Code: ( text )
  1. try: bzip.decompress(test_list)
  2.                except: ReadError
  3.                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?
Would you like to answer this question?
Sign up for a free account, or Login (if you're already a member).
jlm699's Avatar
jlm699
Needs Regular Fix
313 Posts
March 25th, 2008
09:06 PM
#2

Re: Exception handling
You need to define what type of Error you're catching before the colon:
Code: ( text )
  1. try:
  2.     bzip.decompress(test_list)
  3. except ReadError:
  4.     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.

Reply
Thekid's Avatar
Thekid
Member
56 Posts
March 25th, 2008
09:39 PM
#3

Re: Exception handling
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.....

Code: ( text )
  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

Reply
jlm699's Avatar
jlm699
Needs Regular Fix
313 Posts
March 26th, 2008
12:15 PM
#4

Re: Exception handling
What is the error message that you are getting?

Reply
bvdet's Avatar
bvdet
Expert
1,113 Posts
March 26th, 2008
01:24 PM
#5

Re: Exception handling
Thekid,

It looks like you are trying to decompress a list. That should result in a TypeError every time.

Reply
Thekid's Avatar
Thekid
Member
56 Posts
March 26th, 2008
08:22 PM
#6

Re: Exception handling
Quote:
Originally Posted by jlm699
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.

Reply
jlm699's Avatar
jlm699
Needs Regular Fix
313 Posts
March 26th, 2008
08:34 PM
#7

Re: Exception handling
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.

Reply
Thekid's Avatar
Thekid
Member
56 Posts
March 26th, 2008
09:23 PM
#8

Re: Exception handling
Quote:
Originally Posted by jlm699
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.

Reply
Thekid's Avatar
Thekid
Member
56 Posts
March 26th, 2008
10:36 PM
#9

Re: Exception handling
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.

Reply
jlm699's Avatar
jlm699
Needs Regular Fix
313 Posts
March 26th, 2008
11:56 PM
#10

Re: Exception handling
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...

Reply
Thekid's Avatar
Thekid
Member
56 Posts
March 27th, 2008
07:02 PM
#11

Re: Exception handling
Quote:
Originally Posted by jlm699
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.

Reply
jlm699's Avatar
jlm699
Needs Regular Fix
313 Posts
March 27th, 2008
07:29 PM
#12

Re: Exception handling
Quote:
Originally Posted by Thekid
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.

Reply
Reply
Not the answer you were looking for? Post your question . . .
180,783 Experts ready to help you find a solution.
Sign up for a free account, or Login (if you're already a member).

Top Python Forum Contributors