No need to close file?

Discussion in 'Python' started by T, Jul 18, 2006.

  1. T

    T Guest

    Do I need to close the file in this case? Why or why not?

    for line in file('foo', 'r'):
    print line
     
    T, Jul 18, 2006
    #1
    1. Advertising

  2. T enlightened us with:
    > Do I need to close the file in this case? Why or why not?
    >
    > for line in file('foo', 'r'):
    > print line


    Nope, it'll get closed automatically when the file object gets garbage
    collected.

    Sybren
    --
    The problem with the world is stupidity. Not saying there should be a
    capital punishment for stupidity, but why don't we just take the
    safety labels off of everything and let the problem solve itself?
    Frank Zappa
     
    Sybren Stuvel, Jul 18, 2006
    #2
    1. Advertising

  3. T

    Guest

    T> Do I need to close the file in this case? Why or why not?

    T> for line in file('foo', 'r'):
    T> print line

    No. The magic of reference counting.

    S
     
    , Jul 18, 2006
    #3
  4. T

    Guest

    T wrote:
    > Do I need to close the file in this case? Why or why not?
    > for line in file('foo', 'r'):
    > print line


    Close the file in Jython, but often it's not necessary in CPython.

    Bye,
    bearophile
     
    , Jul 18, 2006
    #4
  5. T

    Jarek Zgoda Guest

    T napisa³(a):

    > Do I need to close the file in this case? Why or why not?
    >
    > for line in file('foo', 'r'):
    > print line


    No, if you only read from the file.

    But anyway, closing file object is considered good practice in many
    documents I found, no matter what you do with it.

    --
    Jarek Zgoda
    http://jpa.berlios.de/
     
    Jarek Zgoda, Jul 18, 2006
    #5
  6. I think file object should be closed whether they will be garbage
    collected or not. The same goes for DB and network connections and so
    on. Of course in simple short programs they don't have to, but if
    someone keeps 1000 open files it might be better to close them when
    done. Besides open files (in 'w' mode) might not be able to be opened
    by another process if they are not closed. In general this is usually
    a good habit to have (just like washing dishes right after a meal
    rather than hoping someone will do it later eventually ;)

    Regards,
    Nick V.

    Sybren Stuvel wrote:
    > T enlightened us with:
    > > Do I need to close the file in this case? Why or why not?
    > >
    > > for line in file('foo', 'r'):
    > > print line

    >
    > Nope, it'll get closed automatically when the file object gets garbage
    > collected.
    >
    > Sybren
    > --
    > The problem with the world is stupidity. Not saying there should be a
    > capital punishment for stupidity, but why don't we just take the
    > safety labels off of everything and let the problem solve itself?
    > Frank Zappa
     
    Nick Vatamaniuc, Jul 18, 2006
    #6
  7. "T" <> wrote in message
    news:...
    > Do I need to close the file in this case? Why or why not?
    >
    > for line in file('foo', 'r'):
    > print line


    Are you asking if you can get away without closing it?
    Or are you asking if it is a good idea to not close it?

    Good programming practice says that if you open it - you close it.

    And stay out of trouble ;-)
    Thomas Bartkus
     
    Thomas Bartkus, Jul 18, 2006
    #7
  8. T

    Guest

    T wrote:
    > Do I need to close the file in this case? Why or why not?
    >
    > for line in file('foo', 'r'):
    > print line


    I was running a program in IDLE that opened a file for
    reading and forgot to add the close.

    The program ran and terminated normally.

    But when I tried to open it from Windows Explorer,
    I got the message that it was still in use. Had to close
    IDLE to release it. That wouldn't have happened if I had
    closed it from within the program.
     
    , Jul 18, 2006
    #8
  9. On 2006-07-18, Sybren Stuvel <> wrote:
    > T enlightened us with:
    >> Do I need to close the file in this case? Why or why not?
    >>
    >> for line in file('foo', 'r'):
    >> print line

    >
    > Nope, it'll get closed automatically when the file object gets garbage
    > collected.


    Which might not happen until the program exits. So, for small
    programs, you don't have to close it. Same as C or any other
    language.

    For large or longrunning programs that open lots of files, it's
    generally recommended that you close files when you're done
    with them.

    --
    Grant Edwards grante Yow! I am NOT a nut....
    at
    visi.com
     
    Grant Edwards, Jul 18, 2006
    #9
  10. T

    T Guest

    Thomas Bartkus wrote:
    > "T" <> wrote in message
    > news:...
    > > Do I need to close the file in this case? Why or why not?
    > >
    > > for line in file('foo', 'r'):
    > > print line

    >
    > Are you asking if you can get away without closing it?
    > Or are you asking if it is a good idea to not close it?
    >
    > Good programming practice says that if you open it - you close it.
    >
    > And stay out of trouble ;-)
    > Thomas Bartkus




    How do I close the file in the above case?
     
    T, Jul 18, 2006
    #10
  11. On 2006-07-18, T <> wrote:

    >>> for line in file('foo', 'r'):
    >>> print line


    >> Good programming practice says that if you open it - you close it.
    >>
    >> And stay out of trouble ;-)


    > How do I close the file in the above case?


    Aye, there's the rub.

    You can't close an anonymous file, so you have to give it a name.

    f = file('foo', 'r')
    for line in f:
    print line
    f.close()

    --
    Grant Edwards grante Yow! The PILLSBURY
    at DOUGHBOY is CRYING for
    visi.com an END to BURT REYNOLDS
    movies!!
     
    Grant Edwards, Jul 18, 2006
    #11
  12. T

    Robert Kern Guest

    T wrote:
    > Thomas Bartkus wrote:
    >> "T" <> wrote in message
    >> news:...
    >>> Do I need to close the file in this case? Why or why not?
    >>>
    >>> for line in file('foo', 'r'):
    >>> print line

    >> Are you asking if you can get away without closing it?
    >> Or are you asking if it is a good idea to not close it?
    >>
    >> Good programming practice says that if you open it - you close it.
    >>
    >> And stay out of trouble ;-)
    >> Thomas Bartkus

    >
    > How do I close the file in the above case?


    You rewrite the faulty code such that the above case isn't the above case anymore.

    f = open('foo', 'r')
    try:
    for line in f:
    print line
    finally:
    f.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
     
    Robert Kern, Jul 18, 2006
    #12
  13. There's always the new 'with' statement in Python 2.5. So instead of

    > f = open('foo', 'r')
    > try:
    > for line in f:
    > print line
    > finally:
    > f.close()
    >


    ....you do:

    with open('foo','r') as f:
    for line in f:
    print line

    It's at least a little bit cleaner, and it will close the file if there's an
    exception as well.

    (See http://docs.python.org/dev/whatsnew/pep-343.html and don't forget to include

    from __future__ import with_statement

    at the top of the file)
     
    Kevin Watters, Jul 19, 2006
    #13
  14. T

    Steve Holden Guest

    wrote:
    > T> Do I need to close the file in this case? Why or why not?
    >
    > T> for line in file('foo', 'r'):
    > T> print line
    >
    > No. The magic of reference counting.
    >

    Though of course we have to remember that not all Python implementations
    *use* reference counting. It's certainly true, though, that most Python
    programmers are happy to rely on whatever garbage collector *is*
    implemented to detect the absence of references to the file and close it
    automatically. Or have the operating system do so if the interpreter
    somehow terminates without closing the file.

    I suspect the real answer is "it isn't strictly necessary in modern
    environments, but it can never hurt".

    regards
    Steve
    --
    Steve Holden +44 150 684 7255 +1 800 494 3119
    Holden Web LLC/Ltd http://www.holdenweb.com
    Skype: holdenweb http://holdenweb.blogspot.com
    Recent Ramblings http://del.icio.us/steve.holden
     
    Steve Holden, Jul 19, 2006
    #14
  15. T

    Steve Holden Guest

    wrote:
    > T> Do I need to close the file in this case? Why or why not?
    >
    > T> for line in file('foo', 'r'):
    > T> print line
    >
    > No. The magic of reference counting.
    >

    Though of course we have to remember that not all Python implementations
    *use* reference counting. It's certainly true, though, that most Python
    programmers are happy to rely on whatever garbage collector *is*
    implemented to detect the absence of references to the file and close it
    automatically. Or have the operating system do so if the interpreter
    somehow terminates without closing the file.

    I suspect the real answer is "it isn't strictly necessary in modern
    environments, but it can never hurt".

    regards
    Steve
    --
    Steve Holden +44 150 684 7255 +1 800 494 3119
    Holden Web LLC/Ltd http://www.holdenweb.com
    Skype: holdenweb http://holdenweb.blogspot.com
    Recent Ramblings http://del.icio.us/steve.holden
     
    Steve Holden, Jul 19, 2006
    #15
  16. wrote:
    > T wrote:
    > > Do I need to close the file in this case? Why or why not?
    > >
    > > for line in file('foo', 'r'):
    > > print line

    >
    > I was running a program in IDLE that opened a file for
    > reading and forgot to add the close.
    >
    > The program ran and terminated normally.
    >
    > But when I tried to open it from Windows Explorer,
    > I got the message that it was still in use. Had to close
    > IDLE to release it. That wouldn't have happened if I had
    > closed it from within the program.


    yes, this invariably happens me (with PythonWin) if I try to get away
    without a 'finally'

    Gerard
     
    Gerard Flanagan, Jul 19, 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. Denon
    Replies:
    1
    Views:
    587
    Saravana [MVP]
    Nov 14, 2003
  2. tshad
    Replies:
    1
    Views:
    470
    Scott M.
    Sep 6, 2006
  3. Dani
    Replies:
    3
    Views:
    490
  4. Iñaki Baz Castillo
    Replies:
    7
    Views:
    893
    Iñaki Baz Castillo
    Jan 12, 2010
  5. Iulian Ilea
    Replies:
    1
    Views:
    326
    pcx99
    Dec 21, 2006
Loading...

Share This Page