for line in file('foo', 'r'):
print line
T> for line in file('foo', 'r'):
T> print line
No. The magic of reference counting.
S
Close the file in Jython, but often it's not necessary in CPython.
Bye,
bearophile
> 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/
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
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
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.
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
How do I close the file in the above case?
>>> 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!!
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
> 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)
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
yes, this invariably happens me (with PythonWin) if I try to get away
without a 'finally'
Gerard