[Tutor] Need help appending data to a logfile

Steven D'Aprano steve at pearwood.info
Sun Jun 30 03:38:04 CEST 2013


On 30/06/13 10:51, Alan Gauld wrote:
> On 29/06/13 16:00, Matt D wrote:
>>>>       with tempfile.NamedTemporaryFile('a+t',) as tf:
>>>>           self.logfile = tf
>
> This could give problems.
>
> with guarantees to close the file at the end of the block.
> But you have assigned it to self.logfile.
> So when the file is closed (and tempfile then deletes the
> file on disk) your self.logfile points at... nothing?!

No, it points to a closed file object, which is perfectly legal. And in fact, so does tf.


py> with tempfile.NamedTemporaryFile('a+t',) as tf:
...     pass
...
py> tf
<closed file '<fdopen>', mode 'a+t' at 0xb7f9cd30>


A file object is just a bunch of data such as file name, file mode, pointer to a file, etc. When you close the file, the pointer becomes invalid but the rest of the file object doesn't disappear, it just gets flagged as "closed".


>>>> ValueError: I/O operation on closed file
>
> I have no idea what that means.

It means that Matt is trying to do something input/output related on a closed file that can only be done on an open file, like write to it.



I haven't read this entire thread, but the bits I have read lead me to think that Matt has tangled himself up in a total confused mess. It's *not this hard* to write status messages to a log file, the log module does all the work. Can anyone who has read the thread summarise what Matt is attempting to do?



-- 
Steven


More information about the Tutor mailing list