No need to close file?

T

T

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

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

Sybren Stuvel

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
 
S

skip

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
 
B

bearophileHUGS

T said:
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
 
J

Jarek Zgoda

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

Nick Vatamaniuc

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

Thomas Bartkus

T said:
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
 
M

mensanator

T said:
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.
 
G

Grant Edwards

T enlightened us with:

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

T

Thomas said:
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?
 
G

Grant Edwards

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

Robert Kern

T said:
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
 
K

Kevin Watters

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

Steve Holden

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
 
S

Steve Holden

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
 
G

Gerard Flanagan

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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top