Do I need to close the file in this case? Why or why not?
for line in file('foo', 'r'):
print line 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
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
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/
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
"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
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.
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
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?
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!!
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
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) 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 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 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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
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...
|
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()...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
by: ryjfgjl |
last post by:
ExcelToDatabase: batch import excel into database automatically...
|
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...
|
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...
|
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...
|
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)...
|
by: Defcon1945 |
last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
|
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....
|
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...
|
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...
| |