Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

No need to close file?

1 view
Skip to first unread message

T

unread,
Jul 18, 2006, 3:50:14 PM7/18/06
to
Do I need to close the file in this case? Why or why not?

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

Message has been deleted

sk...@pobox.com

unread,
Jul 18, 2006, 4:04:16 PM7/18/06
to T, pytho...@python.org

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

bearoph...@lycos.com

unread,
Jul 18, 2006, 4:08:45 PM7/18/06
to

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

Bye,
bearophile

Jarek Zgoda

unread,
Jul 18, 2006, 4:16:03 PM7/18/06
to
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/

Nick Vatamaniuc

unread,
Jul 18, 2006, 4:17:32 PM7/18/06
to
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

Thomas Bartkus

unread,
Jul 18, 2006, 4:27:44 PM7/18/06
to

"T" <ty....@yahoo.com> wrote in message
news:1153252214.4...@35g2000cwc.googlegroups.com...

> 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


mensa...@aol.com

unread,
Jul 18, 2006, 4:53:07 PM7/18/06
to

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.

Grant Edwards

unread,
Jul 18, 2006, 5:01:01 PM7/18/06
to
On 2006-07-18, Sybren Stuvel <sybr...@YOURthirdtower.com.imagination> 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

T

unread,
Jul 18, 2006, 5:04:12 PM7/18/06
to

How do I close the file in the above case?

Grant Edwards

unread,
Jul 18, 2006, 5:10:11 PM7/18/06
to
On 2006-07-18, T <ty....@yahoo.com> 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!!

Robert Kern

unread,
Jul 18, 2006, 5:17:50 PM7/18/06
to pytho...@python.org

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

Kevin Watters

unread,
Jul 18, 2006, 9:48:51 PM7/18/06
to pytho...@python.org
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)

Steve Holden

unread,
Jul 19, 2006, 4:01:35 AM7/19/06
to pytho...@python.org, pytho...@python.org
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

unread,
Jul 19, 2006, 4:01:35 AM7/19/06
to sk...@pobox.com, pytho...@python.org

Gerard Flanagan

unread,
Jul 19, 2006, 4:28:54 AM7/19/06
to

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

Gerard

0 new messages