[Tutor] Need to explicitly del a file object after reading contents?

Chad Crabtree flaxeater at yahoo.com
Fri Jun 25 10:57:45 EDT 2004


--- Jeff Kowalczyk <jtk at yahoo.com> wrote:
> I've been explicitly opening, reading, closing and deleting files
> when I
> read their contents into variables. In these cases, I don't need
> the file object kept around for anything. I do this mainly because
> I don't
> know enough about when the garbage collector will delete the file
> object,
> and I wouldn't want it left open until the gc happens.
> 
> Is it necessary to do this:
> 
> f = open(filepath,'r')
> filestring = f.read()
> f.close()
> del f
> 
> vs. this?
> 
> filestring = open(filepath,'r').read()
> 
> The same filepath *may* be read or written in the next few
> seconds/minutes
> by other code, but it usually won't happen. Is there a rule of
> thumb on
> when to go with the explicit file management? Thanks.
> 
I've been told that infact it's ok to do what you wrote.  Python will
note that it's not longer needed and get rid of it in due time.


		
__________________________________
Do you Yahoo!?
Yahoo! Mail - 50x more storage than other providers!
http://promotions.yahoo.com/new_mail



More information about the Tutor mailing list