[newbie] File handle destroyed before pending write is executed?

Peter Otten __peter__ at web.de
Fri Sep 12 09:48:28 CEST 2003


Pieter Lust wrote:

> # main module
> import logmodule
> 
> mylog = logmodule.logfile()
> mylog.enter('something')
> # pass
> # end main module
> 
> # logmodule
> class logfile:
> def __init__(self):
> self.fh = file('log.txt', 'w')
> def __del__(self):
> self.fh.close()
> def enter(self, text):
> self.fh.write('%s\n' % text)
> # end logmodule
> 
> After running the main module, log.txt was empty, though no exceptions
> were raised. To get the text into the file, I had to add the pass
> statement that's commented out above.
> 
> In debug mode, the line cursor jumps from the last line of the main
> module into the __del__() method of the logmodule, but not into the
> enter() method.
> 

Your code works here. I wuuld delete all logmodule.pyc files in your path
and restart the IDE. Maybe there is something out of sync.

(By the way, I think the __del__() method is superfluous)

Peter





More information about the Python-list mailing list