473,327 Members | 1,936 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,327 software developers and data experts.

No need to close file?

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
14 6403

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

"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

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

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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: JatP | last post by:
hi Everyone I am trying to create a server and client to send files from one side to the other. I can send files from one side to the other using bufferedinput/output streams but when trying to...
1
by: ADE | last post by:
Hi everyone well from my last post I found what I am looking for I have some code now that transfers files I have added a GUI to it and need some help with two things one my loadtemplate()...
2
by: Keith Kowalski | last post by:
I anm opening up a text file reading the lines of the file that refer to a tif image in that file, If the tif image does not exist I need it to send an email stating that the file doesn't exist...
2
by: coxnews | last post by:
Hiya, I need to open and read a text based log file in real time as it is being written to by another application. Am using VB.NET in a windows forms application. I have attempted to use a...
4
by: georges the man | last post by:
hey guys, i ve been posting for the last week trying to understand some stuff about c and reading but unfortunaly i couldnt do this. i have to write the following code. this will be the last...
15
by: Gan Quan | last post by:
I'm writing a c++ program that has many (100+) threads read/write files simultaneously. It works well if not considering the efficiency. The file i/o seems to be the bottleneck. This is my code...
46
by: Bruce W. Darby | last post by:
This will be my very first VB.Net application and it's pretty simple. But I've got a snag in my syntax somewhere. Was hoping that someone could point me in the right direction. The history: My...
6
by: Apollo1376 | last post by:
I am very new to C++. I need some help with loading/reading data from text file and write in an array. I have the following data. 12/31/2004 1213.55 1217.33 1211.65 1211.92 786900000 1211.92...
3
by: Eric_Dexter | last post by:
I am trying to take some data in file that looks like this command colnum_1 columnum_2 and look for the command and then cange the value in the collum(word) number indicated. I am under...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.