[Tutor] Need help appending data to a logfile

Dave Angel davea at davea.name
Sat Jun 29 15:15:35 CEST 2013


On 06/29/2013 08:26 AM, Matt D wrote:
>
>>>
>>
>> Matt probably read somewhere about an interface like
>>
>> tempfile.TemporaryFile
>>
>> where the file has no explicit name, and will be deleted from disk, and
>> the space reused as soon as it is closed.
>>
>> I don't believe he's using such an interface, however.
>>
> Yes that is what i was using.
>
>      with tempfile.NamedTemporaryFile('a+t',) as tf:
>          self.logfile = tf
>
> or what i have now:
>
>      self.f = tempfile.NamedTemporaryFile()
>
> 'f' is the file the user opens but it has to be named up by the main
> class constructor or the program gives this error:
>
> ValueError: I/O operation on closed file
>
> so i cant just pass the 'f' from the file dialog into the loop that
> writes the data i need.  at least i have been unable to find a way to do
> this.
>

Look at the keyword arguments to NamedTemporaryFile(), and see if you 
can guess which one you can pass to tell it NOT to delete itself.

Or much better, figure out why you're getting that ValueError exception. 
  Does your file dialog automatically create the file, and only give you 
the open handle?  Or can it be told to return a name string, which 
you'll use where you need it?

Passing an open file handle around in an event-driven system is 
problematic.  Especially using the with-statement semantics.






-- 
DaveA


More information about the Tutor mailing list