Questions about file object and close()

John Marshall John.Marshall at ec.gc.ca
Thu Dec 9 08:09:36 EST 2004


Hi,

Does anyone see a problem with doing:
    data = file("tata").read()

Each time this is done, I see a new file
descriptor allocated (Linux) but not
released.

1) Will there ever be a point where I
   will have a problem with file
   descriptors because the garbage
   collector has _not_ yet collected the
   file objects?

2) When I subclassed the file object as
   follows:
-----
class MyFile(file):
    def close(self):
        print "MyFile.close()"
        file.close(self)
-----
   and did a simple 'MyFile("tata")' I did not see
   a call to MyFile.close(). Am I wrong to have
   expected MyFile.close() to have been called?

3) There is no file.__del__() as far as I
   can tell at the Python level. Are files
   opened by the calls above properly
   closed when the objects are destroyed
   and collected?

Thanks,
John




More information about the Python-list mailing list