[Tutor] How to open the closed file again?
Lie Ryan
lie.1296 at gmail.com
Tue Jan 5 16:10:21 CET 2010
On 1/5/2010 11:46 PM, 朱淳 wrote:
> I've token a dictionary to save filenames, and write it into
> "constant.py". And I think it's a good method to create a class as Andre
> wrote. Thank you all!
> By the way, if I close a open file object, will the closed file object
> still occupy the memory?
Any python object occupies a certain baseline amount of memory.
> As I saw in a list, this kind of unusable file
> object wasn't cleaned by GC.
The file object wasn't cleaned by the GC since you're still holding a
reference to the file object (inside your list or any other variables
currently referencing to it).
> What will happen if there's no list?
Python makes a guarantee that object will never be GC-ed as long as
there is more than one variable referencing the object[1]. If you want
to make sure that an (file) object gets GC-ed you have to delete all
references to it, that is, you have to remove the file object from the
list and delete or reassign all variables currently referencing to the
file object or make the variables out-of-scope. This kind of
micro-management is generally discouraged in python[2].
[1] weak references does not count
[2] it's better to make it so that you don't need to manually delete
variables in the first place and rely on the refcount/GC to delete
variables/object references automatically when they're out of scope
More information about the Tutor
mailing list