473,396 Members | 1,879 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,396 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 6405

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: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.