Exception handling

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Thekid
    New Member
    • Feb 2007
    • 145

    Exception handling

    Hi, I'm having a problem with exception handling....thi s 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:
    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?
  • jlm699
    Contributor
    • Jul 2007
    • 314

    #2
    You need to define what type of Error you're catching before the colon:
    [code=python]
    try:
    bzip.decompress (test_list)
    except ReadError:
    continue
    [/code]
    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.

    Comment

    • Thekid
      New Member
      • Feb 2007
      • 145

      #3
      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:
      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

      Comment

      • jlm699
        Contributor
        • Jul 2007
        • 314

        #4
        What is the error message that you are getting?

        Comment

        • bvdet
          Recognized Expert Specialist
          • Oct 2006
          • 2851

          #5
          Thekid,

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

          Comment

          • Thekid
            New Member
            • Feb 2007
            • 145

            #6
            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\pn gfix.py", line 1, in <module>
            import bzip
            File "C:\Python25\bz ip.py", line 2, in <module>
            tar = tarfile.open('c orrupted[1].png.bz2', 'r:bz2')
            File "C:\Python25\li b\tarfile.py", line 1035, in open
            return func(name, filemode, fileobj)
            File "C:\Python25\li b\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.

            Comment

            • jlm699
              Contributor
              • Jul 2007
              • 314

              #7
              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.

              Comment

              • Thekid
                New Member
                • Feb 2007
                • 145

                #8
                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.

                Comment

                • Thekid
                  New Member
                  • Feb 2007
                  • 145

                  #9
                  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.

                  Comment

                  • jlm699
                    Contributor
                    • Jul 2007
                    • 314

                    #10
                    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...

                    Comment

                    • Thekid
                      New Member
                      • Feb 2007
                      • 145

                      #11
                      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.

                      Comment

                      • jlm699
                        Contributor
                        • Jul 2007
                        • 314

                        #12
                        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.

                        Comment

                        Working...