By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,765 Members | 1,388 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,765 IT Pros & Developers. It's quick & easy.

No need to close file?

P: n/a
T
Do I need to close the file in this case? Why or why not?

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

Jul 18 '06 #1
Share this Question
Share on Google+
14 Replies


P: n/a

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

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

No. The magic of reference counting.

S
Jul 18 '06 #2

P: n/a
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 '06 #3

P: n/a
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/
Jul 18 '06 #4

P: n/a
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
Jul 18 '06 #5

P: n/a

"T" <ty*****@yahoo.comwrote in message
news:11**********************@35g2000cwc.googlegro ups.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
Jul 18 '06 #6

P: n/a

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 '06 #7

P: n/a
On 2006-07-18, Sybren Stuvel <sy*******@YOURthirdtower.com.imaginationwrote:
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
Jul 18 '06 #8

P: n/a
T
Thomas Bartkus wrote:
"T" <ty*****@yahoo.comwrote in message
news:11**********************@35g2000cwc.googlegro ups.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


How do I close the file in the above case?

Jul 18 '06 #9

P: n/a
On 2006-07-18, T <ty*****@yahoo.comwrote:
>>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!!
Jul 18 '06 #10

P: n/a
T wrote:
Thomas Bartkus wrote:
>"T" <ty*****@yahoo.comwrote in message
news:11**********************@35g2000cwc.googlegr oups.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

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

Jul 18 '06 #11

P: n/a
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)

Jul 19 '06 #12

P: n/a
sk**@pobox.com wrote:
TDo I need to close the file in this case? Why or why not?

Tfor 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

Jul 19 '06 #13

P: n/a
sk**@pobox.com wrote:
TDo I need to close the file in this case? Why or why not?

Tfor 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
Jul 19 '06 #14

P: n/a

me********@aol.com 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

Jul 19 '06 #15

This discussion thread is closed

Replies have been disabled for this discussion.