[Tutor] Need help appending data to a logfile

Matt D md123 at nycap.rr.com
Sun Jun 30 05:41:54 CEST 2013


> 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?
> 
Sure np.
So i have a program that gets pickled data from a thread and displays
the data (9 strings) in TextCtrl fields in the UI.  When the pickle is
received it is processed like this:

def display_data(self,event):
    message = event.data
    pickled_dict = message.to_string()
    attrs = pickle.loads(pickled_dict)
    self.update(attrs)

attrs is passed into the update function:

 def update(self, field_values):
    #  logger code----------
    self.logfile.write('\n')
    self.logfile.write('%s,'%(str(strftime("%Y-%m-%d %H:%M:%S",
localtime()))))
    for k,v in self.fields.items():
        f = field_values.get(k, None)
        if f:
            self.logfile.write('%s,'%(str(f)))
     #end logger code ----------------

This loop writes to the text file so it is formatted like this:

time,val1,val2,va3,val4,val5,val6,val7,val8,val9,
time,val1,val2,va3,val4,val5,val6,val7,val8,val9,
time,val1,val2,va3,val4,val5,val6,val7,val8,val9,

so i have the logfile.txt formatted in a was that is very useful for
later inspection with time series analysis.  I just read the logging
module doc including the tutorials and the cookbook, and i have not seen
a way to, in less than 6 lines, create a log file formatted as shown
above.   up near the main class constructor the log file is opened like
this:

    self.logfile = open('logfile.txt', 'a')

it is stored in the home folder and gets appended every time the program
is run.  Only problem is i need multiple log files and the user needs to
be able to choose the file from the UI.  So I put a button on the UI
that and 'binded' to this:

    def openFile(self, evt):
        with wx.FileDialog(self, "Choose a file", os.getcwd(), "",
"*.txt*", wx.OPEN) as dlg:
           if dlg.ShowModal() == wx.ID_OK:
                path = dlg.GetPath()
                mypath = os.path.basename(path)
                uf = open(mypath, "a")
                uf.write(self.logfile)

I have been unable to get the logfile.txt written into the user opened
file.

That is the short version of whats going on in this thread. most of the
posts that were seemingly off topic were dead end attempts to allow the
user to choose what file the information in 'logfile.txt' gets appended.
Thanks!



More information about the Tutor mailing list