[Tutor] Need to explicitly del a file object after reading contents?
jtk at yahoo.com
Fri Jun 25 11:20:38 EDT 2004
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()
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.
More information about the Tutor