[Tutor] Need help appending data to a logfile

Matt D md123 at nycap.rr.com
Sun Jun 30 17:42:18 CEST 2013


On 06/30/2013 10:52 AM, Alan Gauld wrote:
> On 30/06/13 15:04, Matt D wrote:
>>
>> that sounds ideal, but i have not found a way to pass the user selected
>> file into the loop that writes the data without opening another file.
> 
> Don;t pass the file pass the fgile *name*
> Then inside the update method open the file(in append mode)
> and write the latest update then close the file again. (Or
> use the 'with' paradigm which closes the file for you)
> Repeat as needed.
> 
> Part of the propblem is that you are openming the file right
> at the beginning and trying to pass the open file object
> around. Pass the filename and open when needed. Its slightly
> more resource intensive but a lot simpler.
> 
>> how would i use shutil to copy the tempory self.logfile into the user
>> selected file in this open fileFile()?:
> 
> The way to go is to only have one file and avoid all the
> copying stuff completely.
> 
im sorry i don't understand how to pass the file name from the open
dialog into the update method?  I'm sorry i just don't get it.   so if
the user opened file is 'handle' like 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)
                with open('mypath', 'a') as handle:
                    self.handle = handle

Then i put 'handle' in the update() like this??:

def update(self, field_values, handle):
    logfile = open('handle.txt', 'a')

wait no 'handle' is already opened in the click event. so something like
 this:

def update(self, field_values, handle):
    self.logfile = open('logfile.txt' 'a')

    #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 ----------------

     self.logfile = handle

I really have no idea how to go about implementing this idea of passing
the file name around in code.  Any suggestion will be much appreciated.
 Thanks.






More information about the Tutor mailing list