try...except...finally problem in Python 2.5

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • redawgts

    #1

    try...except...finally problem in Python 2.5

    I keep getting this error "local variable 'f' referenced before
    assignment" in the finally block when I run the following code.

    try:
    f = file(self.filen ame, 'rb')
    f.seek(DATA_OFF SET)
    self.__data = f.read(DATA_SIZ E)
    self.isDataLoad ed = True
    except:
    self.isDataLoad ed = False
    finally:
    f.close()

    Can someone tell me what's wrong with the code? Am I doing something
    wrong? I'm somewhat new to python but this makes sense to me.

  • Andrew Koenig

    #2
    Re: try...except... finally problem in Python 2.5

    "redawgts" <redawgts@gmail .comwrote in message
    news:1171482088 .993720.69620@l 53g2000cwa.goog legroups.com...
    >I keep getting this error "local variable 'f' referenced before
    assignment" in the finally block when I run the following code.
    >
    try:
    f = file(self.filen ame, 'rb')
    f.seek(DATA_OFF SET)
    self.__data = f.read(DATA_SIZ E)
    self.isDataLoad ed = True
    except:
    self.isDataLoad ed = False
    finally:
    f.close()
    >
    Can someone tell me what's wrong with the code? Am I doing something
    wrong? I'm somewhat new to python but this makes sense to me.
    If the call to file raises an exception, then variable f won't have been
    created.

    Here's a simple example:

    def f():
    throw 42
    try:
    x = f()
    except:
    print x

    Try it and see what happens. While you're at it, you might try to figure
    out what you would like it to print.



    Comment

    • Robert Kern

      #3
      Re: try...except... finally problem in Python 2.5

      redawgts wrote:
      I keep getting this error "local variable 'f' referenced before
      assignment" in the finally block when I run the following code.
      >
      try:
      f = file(self.filen ame, 'rb')
      f.seek(DATA_OFF SET)
      self.__data = f.read(DATA_SIZ E)
      self.isDataLoad ed = True
      except:
      self.isDataLoad ed = False
      finally:
      f.close()
      >
      Can someone tell me what's wrong with the code? Am I doing something
      wrong? I'm somewhat new to python but this makes sense to me.
      Move the "f = file(self.filen ame, 'rb')" above the try:. If opening the file
      happens to throw an exception, then the assignment will never happen and there
      will be no 'f' to close.

      --
      Robert Kern

      "I have come to believe that the whole world is an enigma, a harmless enigma
      that is made terrible by our own mad attempt to interpret it as though it had
      an underlying truth."
      -- Umberto Eco

      Comment

      • Larry Bates

        #4
        Re: try...except... finally problem in Python 2.5

        redawgts wrote:
        I keep getting this error "local variable 'f' referenced before
        assignment" in the finally block when I run the following code.
        >
        try:
        f = file(self.filen ame, 'rb')
        f.seek(DATA_OFF SET)
        self.__data = f.read(DATA_SIZ E)
        self.isDataLoad ed = True
        except:
        self.isDataLoad ed = False
        finally:
        f.close()
        >
        Can someone tell me what's wrong with the code? Am I doing something
        wrong? I'm somewhat new to python but this makes sense to me.
        >
        finally: block is executed even if there is an exception in which case f
        hasn't been defined. What you want is:

        try:
        f = file(self.filen ame, 'rb')
        f.seek(DATA_OFF SET)
        self.__data = f.read(DATA_SIZ E)
        self.isDataLoad ed = True

        except:
        isDataLoaded=Fa lse

        else:
        f.close()

        -Larry

        Comment

        • Paul Rubin

          #5
          Re: try...except... finally problem in Python 2.5

          "redawgts" <redawgts@gmail .comwrites:
          try:
          f = file(self.filen ame, 'rb') ...
          Can someone tell me what's wrong with the code?
          Various people have explained the error: if the file open attempt
          fails, f is never assigned. Doing it the right way (i.e. handling the
          potential exceptions separately) with try/except statements is messy,
          so it's worth mentioning that 2.5 adds the new "with" statement to
          clean this up. I'm not using 2.5 myself yet so maybe someone will
          have to correct me, but I think you'd write:

          from __future__ import with_statement

          self.isDataLoad ed = False
          with open(self.filen ame, 'rb') as f:
          f.seek(DATA_OFF SET)
          self.__data = f.read(DATA_SIZ E)
          self.isDataLoad ed = True

          and that should handle everything, closing the file automatically.

          Comment

          • redawgts

            #6
            Re: try...except... finally problem in Python 2.5

            Thanks everybody, that helped alot.

            Comment

            • Steven D'Aprano

              #7
              Re: try...except... finally problem in Python 2.5

              On Wed, 14 Feb 2007 12:09:34 -0800, Paul Rubin wrote:
              "redawgts" <redawgts@gmail .comwrites:
              > try:
              > f = file(self.filen ame, 'rb') ...
              >Can someone tell me what's wrong with the code?
              >
              Various people have explained the error: if the file open attempt
              fails, f is never assigned. Doing it the right way (i.e. handling the
              potential exceptions separately) with try/except statements is messy,
              so it's worth mentioning that 2.5 adds the new "with" statement to
              clean this up. I'm not using 2.5 myself yet so maybe someone will
              have to correct me, but I think you'd write:
              >
              from __future__ import with_statement
              >
              self.isDataLoad ed = False
              with open(self.filen ame, 'rb') as f:
              f.seek(DATA_OFF SET)
              self.__data = f.read(DATA_SIZ E)
              self.isDataLoad ed = True
              >
              and that should handle everything, closing the file automatically.

              I don't have Python 2.5 here to experiment, but how is that different from
              this?


              self.isDataLoad ed = False
              try:
              f = open(self.filen ame, 'rb')
              f.seek(DATA_OFF SET)
              self.__data = f.read(DATA_SIZ E)
              self.isDataLoad ed = True
              except:
              pass
              else:
              pass

              (apart from being four lines shorter)



              --
              Steven D'Aprano

              Comment

              • Paul Rubin

                #8
                Re: try...except... finally problem in Python 2.5

                Steven D'Aprano <steve@REMOVEME .cybersource.co m.auwrites:
                self.isDataLoad ed = False
                try:
                f = open(self.filen ame, 'rb')
                f.seek(DATA_OFF SET)
                self.__data = f.read(DATA_SIZ E)
                self.isDataLoad ed = True
                except:
                pass
                else:
                pass
                >
                (apart from being four lines shorter)
                Your version never closes the file.

                Comment

                • Steven D'Aprano

                  #9
                  Re: try...except... finally problem in Python 2.5

                  On Wed, 14 Feb 2007 18:03:19 -0800, Paul Rubin wrote:
                  Steven D'Aprano <steve@REMOVEME .cybersource.co m.auwrites:
                  >self.isDataLoa ded = False
                  >try:
                  > f = open(self.filen ame, 'rb')
                  > f.seek(DATA_OFF SET)
                  > self.__data = f.read(DATA_SIZ E)
                  > self.isDataLoad ed = True
                  >except:
                  > pass
                  >else:
                  > pass
                  >>
                  >(apart from being four lines shorter)
                  >
                  Your version never closes the file.

                  Yes it does. Eventually f goes out of scope and is closed automatically.

                  I don't see where the "with" version closes the file either. How does it
                  know that I want to call the f's close() method, rather than, say,
                  f.exit() or f.do_something_ else()?



                  --
                  Steven D'Aprano

                  Comment

                  • Steven D'Aprano

                    #10
                    Re: try...except... finally problem in Python 2.5

                    Replying to my own question...

                    On Thu, 15 Feb 2007 13:17:00 +1100, Steven D'Aprano wrote:
                    I don't see where the "with" version closes the file either. How does it
                    know that I want to call the f's close() method, rather than, say,
                    f.exit() or f.do_something_ else()?
                    Ah, I *think* I see... file objects in Python 2.5 are objects that know
                    how to work with the "with" statement; that is they obey the "context
                    management" protocol and have __enter__ and __exit__ methods that do the
                    right thing to make everything work correctly.



                    Have I got it right?


                    --
                    Steven D'Aprano

                    Comment

                    • Paul Rubin

                      #11
                      Re: try...except... finally problem in Python 2.5

                      Steven D'Aprano <steve@REMOVEME .cybersource.co m.auwrites:
                      Yes it does. Eventually f goes out of scope and is closed automatically.
                      Oh right, however you can't really predict when the closure occurs,
                      unless you're relying on current CPython artifacts.

                      Re your other post: yes, PEP 363 explains how the "with" statement
                      calls the __exit__ method.

                      Comment

                      • Paul Rubin

                        #12
                        Re: try...except... finally problem in Python 2.5

                        Paul Rubin <http://phr.cx@NOSPAM.i nvalidwrites:
                        Re your other post: yes, PEP 363 explains how the "with" statement
                        Whoops, 343.

                        Comment

                        Working...