[Tutor] Need help appending data to a logfile

Matt D md123 at nycap.rr.com
Fri Jun 28 23:25:18 CEST 2013


> But I must say this seems like an inordinately complicated way
> of doing things and quite out of the norm for other applications.
> Can you back up several steps and explain again what exactly you
> are trying to achieve? What is the user experience supposed to
> be? What is being recorded where and why? How does a user
> interact with it?
> 
> Logging is such a well established process for most applications that
> one of a very few patterns are usually followed. You seem to be
> trying to invent a whole new paradigm here? (And that may not be
> a bad thing, but we should at least understand why it's needed!)
> 

Sure. so to to be perfectly clear here, about what i am doing and what i
where i want to get to.  currently i have a little code that logs (I say
logging but its not the debugging thing) data that gets sent to the
Python UI.  I have a thread that waits on new data and when it comes in
it gets displayed in TextCtrl fields.  every time this happens my logger
puts those values into a text file one row at a time.

this how i open and the file for logging data:

    # open a file named
    "logfile.txt" in "a" append mode;
    self.logfile = open('logfile.txt', 'a')

and i have a loop that writes 8 comma separated values in row with this:

    self.logfile.write('%s,'%(str(f))

however, using this setup the user cannot select the file to save the
log to, it just gets written into the home folder.  so i came up with
this is to allow the user can open a file from the UI:

#open file dialog -----------------------------
    def openFile(self, evt):
        with wx.FileDialog(self, "Choose a file", os.getcwd(), "",
"*.*", wx.OPEN) as dlg:
           if dlg.ShowModal() == wx.ID_OK:
                path = dlg.GetPath()
                mypath = os.path.basename(path)
                #with open(mypath, "a") as f:

but i hadn't figured a way to get what is in the 'logfile.txt' into the
file the user opens with the file dialog.  so i was trying this:

import tempfile

self.logfile = tempfile.NamedTemporaryFile()

to open the tempfile hoping it would behave exactly as the old
'self.logfile' did in terms of getting the data written to it.
And i tried what you already saw to get the contents of the temporary
file 'self.logfile' copied or written into the file the user opens from
the UI.  Now I am testing your suggestion:

def openFile(self, evt):
        with wx.FileDialog(self, "Choose a file", os.getcwd(), "",
"*.txt*", wx.SAVE) as dlg:
           if dlg.ShowModal() == wx.ID_OK:
                path = dlg.GetPath()
                mypath = os.path.basename(path)
                with open(mypath, "a") as f:
                    self.logfile.seek(0)#start at beginning
                    f.write(self.logfile.read())
                    self.logfile.close()

i am trying to allow the user to open a file and then have whats in the
temp logfile written into the file the user selected from the file
dialog. or appended actually.  and then obviously the tempfile is closed
which i was under the impression would destroy the file automatically
and free up the memory.
I have been trying for a while to find some standard why of achieving
this functionality but have been unable to find any examples.
Thanks!







More information about the Tutor mailing list