Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > No need to close file?

Reply
Thread Tools

No need to close file?

 
 
T
Guest
Posts: n/a
 
      07-18-2006
Do I need to close the file in this case? Why or why not?

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

 
Reply With Quote
 
 
 
 
Sybren Stuvel
Guest
Posts: n/a
 
      07-18-2006
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
 
Reply With Quote
 
 
 
 
skip@pobox.com
Guest
Posts: n/a
 
      07-18-2006

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
 
Reply With Quote
 
bearophileHUGS@lycos.com
Guest
Posts: n/a
 
      07-18-2006
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

 
Reply With Quote
 
Jarek Zgoda
Guest
Posts: n/a
 
      07-18-2006
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/
 
Reply With Quote
 
Nick Vatamaniuc
Guest
Posts: n/a
 
      07-18-2006
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


 
Reply With Quote
 
Thomas Bartkus
Guest
Posts: n/a
 
      07-18-2006

"T" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) 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


 
Reply With Quote
 
mensanator@aol.com
Guest
Posts: n/a
 
      07-18-2006

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.

 
Reply With Quote
 
Grant Edwards
Guest
Posts: n/a
 
      07-18-2006
On 2006-07-18, Sybren Stuvel <(E-Mail Removed)> 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.


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
 
Reply With Quote
 
T
Guest
Posts: n/a
 
      07-18-2006
Thomas Bartkus wrote:
> "T" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed) 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?

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
How to close a TCP socket? (TCPSocket#close doesn't close it) IƱaki Baz Castillo Ruby 7 01-12-2010 01:32 PM
Do I need to close Reader if I close the Connection? tshad ASP .Net 1 09-06-2006 12:15 AM
Why does JdbcRowSetImpl.close() close the database connection? Paul van Rossem Java 0 04-07-2005 07:01 PM
JavaMail POP3 folder.close() method close also store! Daniel Albisser Java 1 04-07-2004 03:45 PM
How to close child browser while parent close? Denon ASP .Net 1 11-14-2003 08:14 AM



Advertisments