[Tutor] Writing logfile data to a user opened file

Matt D md123 at nycap.rr.com
Fri Jun 21 21:12:55 CEST 2013


> 
> When you open a file the data should be written to that. If you want to
> move existing data from logfile.txt into user opened file then you need
> to read logfile.txt and then write it to the user opened file. To make
> your life simpler, either pass in the file path or open the file save
> dialog on __init__.
> 
> 
> ~Ramit
> 
I got so frustrated try to figure a way to use the logfile.txt that I
changed how i log. first i name an array:

class TrafficPane(wx.Panel):
    # Initializer
    # the class constructor
    def __init__(self, parent, msgq):
        wx.Panel.__init__(self, parent)
        self.msgq = msgq
        #create the array to put the traffic data in
        self.log_array = []

Then this is how the array gets into the file the user chooses:

  # openfile defined to start FileDialog
    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)
                mypath = os.path.abspath(path)
                f = open(mypath, "rw+")
                f.writelines(self.log_array)

And this is how i get the TextCtrl values into the array:

def update(self, field_values):
        next_line = ""
        #logger code---------------
        #  first new line
        #self.logfile.write('\n')
        #  date and time
        #self.logfile.write('%s,'%(str(strftime("%Y-%m-%d %H:%M:%S",
localtime()))))
        next_line += (str(strftime("%Y-%m-%d %H:%M:%S", localtime())))
        #  loop through each of the TextCtrl objects
        for k,v in self.fields.items():
            #  get the value of the current TextCtrl field
            f = field_values.get(k, None)
            if f:
                #  output the value with trailing comma
                #self.logfile.write('%s,'%(str(f)))
                next_line += (str(f) + ',')
        log_array.append(next_line)
        #end logger code----------

Its running right now.  I haven't had the opportunity to test if it
works so im keeping my fingers crossed.


More information about the Tutor mailing list