Opening files without closing them

Discussion in 'Python' started by Sandra-24, Mar 5, 2006.

  1. Sandra-24

    Sandra-24 Guest

    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
     
    Sandra-24, Mar 5, 2006
    #1
    1. Advertising

  2. Sandra-24 wrote:
    > 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()
    >


    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
     
    =?UTF-8?B?TWFyY2luIE1pZWzFvHnFhHNraQ==?=, Mar 5, 2006
    #2
    1. Advertising

  3. Sandra-24

    Robert Kern Guest

    Sandra-24 wrote:
    > 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)?


    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.

    > 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?


    Just keep doing what you are doing, please.

    --
    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
     
    Robert Kern, Mar 5, 2006
    #3
  4. Marcin Mielżyński wrote:
    > Sandra-24 wrote:
    >> 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()
    >>

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


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

    sorry for spam

    lopex
     
    =?UTF-8?B?TWFyY2luIE1pZWzFvHnFhHNraQ==?=, Mar 5, 2006
    #4
  5. Robert Kern wrote:

    >> 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?

    >
    > Just keep doing what you are doing, please.


    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 && && 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
     
    Erik Max Francis, Mar 5, 2006
    #5
  6. Sandra-24

    Robert Kern Guest

    Erik Max Francis wrote:
    > Robert Kern wrote:
    >
    >>>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?

    >>
    >>Just keep doing what you are doing, please.

    >
    > 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()


    Yes, you are correct.

    --
    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
     
    Robert Kern, Mar 6, 2006
    #6
  7. Sandra-24 wrote:
    > 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?


    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
    >>> from __future__ import with_statement
    >>> with open('readme.txt') as f:

    .... contents = f.read()
    ....
    >>> f

    <closed file 'readme.txt', mode 'r' at 0x00B8BAA8>

    Of course, you have to wait until August or so for Python 2.5:
    http://www.python.org/peps/pep-0356.html

    STeVe
     
    Steven Bethard, Mar 6, 2006
    #7
  8. Sandra-24 a écrit :
    > 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)?


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

    > I usually use:
    >
    > try:
    > f = open(file)
    > contents = f.read()
    > finally:
    > f.close()
    >
    > But now I am wondering if that is the same thing.


    Not quite:

    >>> try:

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


    > Which method would
    > you rather use?


    For a quick script, the simplest one. For production code, it depends
    too much on the context to give a definitive single answer.
     
    Bruno Desthuilliers, Mar 6, 2006
    #8
  9. Sandra-24

    3c273 Guest

    "Erik Max Francis" <> wrote in message
    news:...
    > 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()
    >

    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
     
    3c273, Mar 6, 2006
    #9
  10. Sandra-24

    Paul Rubin Guest

    "3c273" <> writes:
    > > f = open(file)
    > > try:
    > > contents = f.read()
    > > finally:
    > > f.close()
    > >

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


    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.
     
    Paul Rubin, Mar 6, 2006
    #10
  11. Sandra-24

    Robert Kern Guest

    Paul Rubin wrote:
    > "3c273" <> writes:
    >
    >>>f = open(file)
    >>>try:
    >>> contents = f.read()
    >>>finally:
    >>> f.close()
    >>>

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

    >
    > 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 of
    the exception that you're really interested in seeing.

    --
    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
     
    Robert Kern, Mar 6, 2006
    #11
  12. Sandra-24

    3c273 Guest

    "Robert Kern" <> wrote in message
    news:...
    > 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

    of
    > the exception that you're really interested in seeing.


    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_else_instead()

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

    Thanks again.
    Louis
     
    3c273, Mar 7, 2006
    #12
  13. Sandra-24

    Peter Hansen Guest

    3c273 wrote:
    > "Robert Kern" <> wrote in message
    > news:...
    >
    >>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

    >
    > of
    >
    >>the exception that you're really interested in seeing.

    >
    >
    > 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_else_instead()
    >
    > try:
    > contents = f.read()
    > finally:
    > f.close()


    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
     
    Peter Hansen, Mar 7, 2006
    #13
  14. Sandra-24

    Bryan Guest

    Peter Hansen wrote:
    > 3c273 wrote:
    >> "Robert Kern" <> wrote in message
    >> news:...
    >>
    >>> 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

    >> of
    >>
    >>> the exception that you're really interested in seeing.

    >>
    >> 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_else_instead()
    >>
    >> try:
    >> contents = f.read()
    >> finally:
    >> f.close()

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


    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
     
    Bryan, Mar 8, 2006
    #14
  15. Sandra-24

    3c273 Guest

    "Peter Hansen" <> wrote in message
    news:...
    >
    > 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.
    >

    Thank you for your reply. I had forgotten that you could use 'else' in a
    'try' statement. I like this solution. Thanks again.
    Louis
     
    3c273, Mar 8, 2006
    #15
  16. Sandra-24

    3c273 Guest

    "Bryan" <> wrote in message
    news:...
    > 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
    >

    I like this idea also. Thanks to all who helped me understand..
    Louis
     
    3c273, Mar 8, 2006
    #16
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. ton
    Replies:
    1
    Views:
    409
    Bryan Donaldson
    Feb 11, 2004
  2. =?Utf-8?B?S2FubmFuLlYgW01DU0QubmV0XQ==?=

    Printing PDF documents without actually opening them.....

    =?Utf-8?B?S2FubmFuLlYgW01DU0QubmV0XQ==?=, Apr 26, 2005, in forum: ASP .Net
    Replies:
    1
    Views:
    838
    Kevin Spencer
    Apr 26, 2005
  3. Anonieko

    HttpHandlers - Learn Them. Use Them.

    Anonieko, Jun 15, 2006, in forum: ASP .Net
    Replies:
    5
    Views:
    525
    tdavisjr
    Jun 16, 2006
  4. why the lucky stiff
    Replies:
    5
    Views:
    148
    why the lucky stiff
    Sep 22, 2004
  5. thomas
    Replies:
    0
    Views:
    229
    thomas
    Oct 23, 2003
Loading...

Share This Page