[Tutor] Need help appending data to a logfile
Matt D
md123 at nycap.rr.com
Sat Jun 29 17:00:52 CEST 2013
On 06/29/2013 09:15 AM, Dave Angel wrote:
> 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.
>
So i should tell it NOT to delete itself? i was under the impression
that having it delete itself upon program close is a good thing. i
thought the point of not using variables for this sort of thing is to
save memory space. there will be a time when this program is left
running for days if not weeks.
the value error is from trying to write to a file that is not open.
apparently the file dialog is not opening the file in way that i can use
like that. having the file dialog be able to return the file name that
the user opens and having that file available to the loop that writes
the data would be ideal. trying to figure a way to do this has been the
source of my frustration for a long time now.
More information about the Tutor
mailing list