Opening files without closing them

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Sandra-24

    Opening files without closing them

    I was reading over some python code recently, and I saw something like
    this:

    contents = open(file).read ()

    And of course you can also do:

    open(file, "w").write( obj)

    Why do they no close the files? Is this sloppy programming or is the
    file automatically closed when the reference is destroyed (after this
    line)? I usually use:

    try:
    f = open(file)
    contents = f.read()
    finally:
    f.close()

    But now I am wondering if that is the same thing. Which method would
    you rather use? Why?

    Thanks,
    Sandra

  • Marcin MielżyÅ„ski

    #2
    Re: Opening files without closing them

    Sandra-24 wrote:[color=blue]
    > I was reading over some python code recently, and I saw something like
    > this:
    >
    > contents = open(file).read ()
    >
    > And of course you can also do:
    >
    > open(file, "w").write( obj)
    >
    > Why do they no close the files? Is this sloppy programming or is the
    > file automatically closed when the reference is destroyed (after this
    > line)? I usually use:
    >
    > try:
    > f = open(file)
    > contents = f.read()
    > finally:
    > f.close()
    >[/color]

    this above is equivalent to:

    open(file){|f|
    contents=f.read
    }

    the logic taking care of everything is encapsulated in open.

    but can be done in less ruby way way :)



    lopex

    Comment

    • Marcin MielżyÅ„ski

      #3
      Re: Opening files without closing them

      Marcin Mielżyński wrote:[color=blue]
      > Sandra-24 wrote:[color=green]
      >> I was reading over some python code recently, and I saw something like
      >> this:
      >>
      >> contents = open(file).read ()
      >>
      >> And of course you can also do:
      >>
      >> open(file, "w").write( obj)
      >>
      >> Why do they no close the files? Is this sloppy programming or is the
      >> file automatically closed when the reference is destroyed (after this
      >> line)? I usually use:
      >>
      >> try:
      >> f = open(file)
      >> contents = f.read()
      >> finally:
      >> f.close()
      >>[/color]
      >
      > this above is equivalent to:
      >
      > open(file){|f|
      > contents=f.read
      > }
      >
      > the logic taking care of everything is encapsulated in open.
      >
      > but can be done in less ruby way way :)
      >
      >
      >
      > lopex[/color]

      Oops I thought I was writing to c.l.ruby :D

      sorry for spam

      lopex

      Comment

      • Robert Kern

        #4
        Re: Opening files without closing them

        Sandra-24 wrote:[color=blue]
        > I was reading over some python code recently, and I saw something like
        > this:
        >
        > contents = open(file).read ()
        >
        > And of course you can also do:
        >
        > open(file, "w").write( obj)
        >
        > Why do they no close the files? Is this sloppy programming or is the
        > file automatically closed when the reference is destroyed (after this
        > line)?[/color]

        Both!

        Usually, the files will probably be closed *if* you are using CPython. However,
        there is absolutely no guarantee of this behavior. For example, Jython uses a
        different garbage collection scheme, and the files will *not* close immediately.
        Future versions of CPython may have different behavior, too.
        [color=blue]
        > I usually use:
        >
        > try:
        > f = open(file)
        > contents = f.read()
        > finally:
        > f.close()
        >
        > But now I am wondering if that is the same thing. Which method would
        > you rather use? Why?[/color]

        Just keep doing what you are doing, please.

        --
        Robert Kern
        robert.kern@gma il.com

        "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

        • Bruno Desthuilliers

          #5
          Re: Opening files without closing them

          Sandra-24 a écrit :[color=blue]
          > I was reading over some python code recently, and I saw something like
          > this:
          >
          > contents = open(file).read ()
          >
          > And of course you can also do:
          >
          > open(file, "w").write( obj)
          >
          > Why do they no close the files? Is this sloppy programming or is the
          > file automatically closed when the reference is destroyed (after this
          > line)?[/color]

          IIRC, the current CPython implementation takes care of closing file
          objects that are no longer referenced. But this may not be true of other
          implementations (think: Jython).
          [color=blue]
          > I usually use:
          >
          > try:
          > f = open(file)
          > contents = f.read()
          > finally:
          > f.close()
          >
          > But now I am wondering if that is the same thing.[/color]

          Not quite:
          [color=blue][color=green][color=darkred]
          >>> try:[/color][/color][/color]
          .... f = open('file_that _doesnt_exists. ext')
          .... finally:
          .... f.close()
          ....
          Traceback (most recent call last):
          File "<stdin>", line 4, in ?
          NameError: name 'f' is not defined[color=blue][color=green][color=darkred]
          >>>[/color][/color][/color]
          [color=blue]
          > Which method would
          > you rather use?[/color]

          For a quick script, the simplest one. For production code, it depends
          too much on the context to give a definitive single answer.

          Comment

          • Erik Max Francis

            #6
            Re: Opening files without closing them

            Robert Kern wrote:
            [color=blue][color=green]
            >> I usually use:
            >>
            >> try:
            >> f = open(file)
            >> contents = f.read()
            >> finally:
            >> f.close()
            >>
            >> But now I am wondering if that is the same thing. Which method would
            >> you rather use? Why?[/color]
            >
            > Just keep doing what you are doing, please.[/color]

            Note quite. The assignment of the resources to its variable needs to be
            done before the try:

            f = open(file)
            try:
            contents = f.read()
            finally:
            f.close()

            --
            Erik Max Francis && max@alcyone.com && http://www.alcyone.com/max/
            San Jose, CA, USA && 37 20 N 121 53 W && AIM erikmaxfrancis
            Who, my friend, can scale heaven?
            -- _Gilgamesh_, ca. 3rd C. BC

            Comment

            • Robert Kern

              #7
              Re: Opening files without closing them

              Erik Max Francis wrote:[color=blue]
              > Robert Kern wrote:
              >[color=green][color=darkred]
              >>>I usually use:
              >>>
              >>>try:
              >>> f = open(file)
              >>> contents = f.read()
              >>>finally:
              >>> f.close()
              >>>
              >>>But now I am wondering if that is the same thing. Which method would
              >>>you rather use? Why?[/color]
              >>
              >>Just keep doing what you are doing, please.[/color]
              >
              > Note quite. The assignment of the resources to its variable needs to be
              > done before the try:
              >
              > f = open(file)
              > try:
              > contents = f.read()
              > finally:
              > f.close()[/color]

              Yes, you are correct.

              --
              Robert Kern
              robert.kern@gma il.com

              "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

              • Steven Bethard

                #8
                Re: Opening files without closing them

                Sandra-24 wrote:[color=blue]
                > I was reading over some python code recently, and I saw something like
                > this:
                >
                > contents = open(file).read ()
                >
                > And of course you can also do:
                >
                > open(file, "w").write( obj)
                >
                > Why do they no close the files? Is this sloppy programming or is the
                > file automatically closed when the reference is destroyed (after this
                > line)? I usually use:
                >
                > try:
                > f = open(file)
                > contents = f.read()
                > finally:
                > f.close()
                >
                > But now I am wondering if that is the same thing. Which method would
                > you rather use? Why?[/color]

                In Python 2.5, you'll write::

                with open(file) as f:
                contents = f.read()

                and Python will automatically close the file at the end of the
                with-statement. Observe:

                Python 2.5a0 (trunk:42857M, Mar 5 2006, 14:50:28) [MSC v.1310 32 bit
                (Intel)] on win32[color=blue][color=green][color=darkred]
                >>> from __future__ import with_statement
                >>> with open('readme.tx t') as f:[/color][/color][/color]
                .... contents = f.read()
                ....[color=blue][color=green][color=darkred]
                >>> f[/color][/color][/color]
                <closed file 'readme.txt', mode 'r' at 0x00B8BAA8>

                Of course, you have to wait until August or so for Python 2.5:
                This document describes the development and release schedule for Python 2.5. The schedule primarily concerns itself with PEP-sized items. Small features may be added up to and including the first beta release. Bugs may be fixed until the final release.


                STeVe

                Comment

                • 3c273

                  #9
                  Re: Opening files without closing them

                  "Erik Max Francis" <max@alcyone.co m> wrote in message
                  news:wPCdnSJq7O bP5pbZnZ2dnUVZ_ sSdnZ2d@speakea sy.net...[color=blue]
                  > Note quite. The assignment of the resources to its variable needs to be
                  > done before the try:
                  >
                  > f = open(file)
                  > try:
                  > contents = f.read()
                  > finally:
                  > f.close()
                  >[/color]
                  Pardon the newbie question, but could you explain why? I have been doing it
                  the same way as the OP and would like to know the difference. Thank you.
                  Louis


                  Comment

                  • Paul Rubin

                    #10
                    Re: Opening files without closing them

                    "3c273" <nospam@nospam. com> writes:[color=blue][color=green]
                    > > f = open(file)
                    > > try:
                    > > contents = f.read()
                    > > finally:
                    > > f.close()
                    > >[/color]
                    > Pardon the newbie question, but could you explain why? I have been doing it
                    > the same way as the OP and would like to know the difference. Thank you.[/color]

                    Say that the open is inside the try block. If the file can't be
                    opened, then 'open' raises an exception, 'f' doesn't get set, and then
                    the 'finally' clause tries to close f. f might have been previously
                    bound to some other file (which still has other handles alive) and so
                    the wrong file gets closed.

                    Comment

                    • Robert Kern

                      #11
                      Re: Opening files without closing them

                      Paul Rubin wrote:[color=blue]
                      > "3c273" <nospam@nospam. com> writes:
                      >[color=green][color=darkred]
                      >>>f = open(file)
                      >>>try:
                      >>> contents = f.read()
                      >>>finally:
                      >>> f.close()
                      >>>[/color]
                      >>
                      >>Pardon the newbie question, but could you explain why? I have been doing it
                      >>the same way as the OP and would like to know the difference. Thank you.[/color]
                      >
                      > Say that the open is inside the try block. If the file can't be
                      > opened, then 'open' raises an exception, 'f' doesn't get set, and then
                      > the 'finally' clause tries to close f. f might have been previously
                      > bound to some other file (which still has other handles alive) and so
                      > the wrong file gets closed.[/color]

                      And even if 'f' wasn't bound to anything, you will get a NameError instead of
                      the exception that you're really interested in seeing.

                      --
                      Robert Kern
                      robert.kern@gma il.com

                      "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

                      • 3c273

                        #12
                        Re: Opening files without closing them

                        "Robert Kern" <robert.kern@gm ail.com> wrote in message
                        news:mailman.27 94.1141683892.2 7775.python-list@python.org ...[color=blue]
                        > Paul Rubin wrote:[color=green]
                        > > Say that the open is inside the try block. If the file can't be
                        > > opened, then 'open' raises an exception, 'f' doesn't get set, and then
                        > > the 'finally' clause tries to close f. f might have been previously
                        > > bound to some other file (which still has other handles alive) and so
                        > > the wrong file gets closed.[/color]
                        >
                        > And even if 'f' wasn't bound to anything, you will get a NameError instead[/color]
                        of[color=blue]
                        > the exception that you're really interested in seeing.[/color]

                        Thanks to both of you. So in order to be thorough, should I be doing:
                        try:
                        f=open('file')
                        except: IOError:
                        print 'doesn't exist'
                        so_something_el se_instead()

                        try:
                        contents = f.read()
                        finally:
                        f.close()

                        Thanks again.
                        Louis


                        Comment

                        • Peter Hansen

                          #13
                          Re: Opening files without closing them

                          3c273 wrote:[color=blue]
                          > "Robert Kern" <robert.kern@gm ail.com> wrote in message
                          > news:mailman.27 94.1141683892.2 7775.python-list@python.org ...
                          >[color=green]
                          >>Paul Rubin wrote:
                          >>[color=darkred]
                          >>>Say that the open is inside the try block. If the file can't be
                          >>>opened, then 'open' raises an exception, 'f' doesn't get set, and then
                          >>>the 'finally' clause tries to close f. f might have been previously
                          >>>bound to some other file (which still has other handles alive) and so
                          >>>the wrong file gets closed.[/color]
                          >>
                          >>And even if 'f' wasn't bound to anything, you will get a NameError instead[/color]
                          >
                          > of
                          >[color=green]
                          >>the exception that you're really interested in seeing.[/color]
                          >
                          >
                          > Thanks to both of you. So in order to be thorough, should I be doing:
                          > try:
                          > f=open('file')
                          > except: IOError:
                          > print 'doesn't exist'
                          > so_something_el se_instead()
                          >
                          > try:
                          > contents = f.read()
                          > finally:
                          > f.close()[/color]

                          Unfortunately, that would still have trouble if the first exception
                          handler was executed, since then you'd try read from f, which would fail
                          with another exception, and then you'd try to close f, and that would
                          probably fail and raise an exception that isn't caught anywhere.

                          So this is better, though probably excessive in small scripts:

                          try:
                          f = open('file')
                          except IOError:
                          # do something else
                          else:
                          try:
                          content = f.read()
                          finally:
                          f.close()

                          This takes advantage of "else" on try statements, which executes only if
                          the except statement is not executed.

                          -Peter

                          Comment

                          • Bryan

                            #14
                            Re: Opening files without closing them

                            Peter Hansen wrote:[color=blue]
                            > 3c273 wrote:[color=green]
                            >> "Robert Kern" <robert.kern@gm ail.com> wrote in message
                            >> news:mailman.27 94.1141683892.2 7775.python-list@python.org ...
                            >>[color=darkred]
                            >>> Paul Rubin wrote:
                            >>>
                            >>>> Say that the open is inside the try block. If the file can't be
                            >>>> opened, then 'open' raises an exception, 'f' doesn't get set, and then
                            >>>> the 'finally' clause tries to close f. f might have been previously
                            >>>> bound to some other file (which still has other handles alive) and so
                            >>>> the wrong file gets closed.
                            >>> And even if 'f' wasn't bound to anything, you will get a NameError instead[/color]
                            >> of
                            >>[color=darkred]
                            >>> the exception that you're really interested in seeing.[/color]
                            >>
                            >> Thanks to both of you. So in order to be thorough, should I be doing:
                            >> try:
                            >> f=open('file')
                            >> except: IOError:
                            >> print 'doesn't exist'
                            >> so_something_el se_instead()
                            >>
                            >> try:
                            >> contents = f.read()
                            >> finally:
                            >> f.close()[/color]
                            >
                            > Unfortunately, that would still have trouble if the first exception
                            > handler was executed, since then you'd try read from f, which would fail
                            > with another exception, and then you'd try to close f, and that would
                            > probably fail and raise an exception that isn't caught anywhere.
                            >
                            > So this is better, though probably excessive in small scripts:
                            >
                            > try:
                            > f = open('file')
                            > except IOError:
                            > # do something else
                            > else:
                            > try:
                            > content = f.read()
                            > finally:
                            > f.close()
                            >
                            > This takes advantage of "else" on try statements, which executes only if
                            > the except statement is not executed.
                            >
                            > -Peter
                            >[/color]

                            this is what i always do for files and other types of resources:


                            if it's a low-level routine, i usually let any exceptions bubble up to a higher
                            level routine that cares or knows what to do.

                            f = open('file')
                            try:
                            # do something
                            finally:
                            f.close()


                            if i really want to handle the exception, then i handle it at a conceptually
                            "higher" level by wrapping it in an exception which is basically what some
                            higher-level routine would do anyways.


                            try:
                            f = open('file)
                            try:
                            # do something
                            finally:
                            f.close()
                            except IOError:
                            # handle exceptions



                            bryan

                            Comment

                            • 3c273

                              #15
                              Re: Opening files without closing them

                              "Peter Hansen" <peter@engcorp. com> wrote in message
                              news:mailman.28 50.1141759783.2 7775.python-list@python.org ...[color=blue]
                              >
                              > So this is better, though probably excessive in small scripts:
                              >
                              > try:
                              > f = open('file')
                              > except IOError:
                              > # do something else
                              > else:
                              > try:
                              > content = f.read()
                              > finally:
                              > f.close()
                              >
                              > This takes advantage of "else" on try statements, which executes only if
                              > the except statement is not executed.
                              >[/color]
                              Thank you for your reply. I had forgotten that you could use 'else' in a
                              'try' statement. I like this solution. Thanks again.
                              Louis


                              Comment

                              Working...