[Tutor] Need help appending data to a logfile

Alan Gauld alan.gauld at btinternet.com
Sun Jun 30 19:43:04 CEST 2013


On 30/06/13 16:42, Matt D wrote:

> 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?:

OK, technically you probably donb;t want to pasws it into the metjod but 
to store it in an attribute of the object.

>   def openFile(self, evt):
>          with wx.FileDialog(self, "Choose a file", os.getcwd(), "",
> "*.txt*", wx.OPEN) as dlg:
>             if dlg.ShowModal() == wx.ID_OK:
>                  self.logpath = dlg.GetPath()

Now self.logpath holds a record of the full path to the log file.
(assuming dlg.getPath does return the full path, I haven't
used wxPython for a while...)

> def update(self, field_values):
>      with open(self.logpath, 'a')  as logfile:
 >          # format the line and write to file as before

And that is all you need.
logfile is now your open logfile object referencing the file named by 
the user in the dialog. Because we opened it in a 'with' construct it 
will automatically be closed at the end of the block ready to be 
reopened next time update gets called.

And that is it. No more should be required. Provided you have permission 
to write to the file/folder then you are now logging
to the file specified by the user.

-- 
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/



More information about the Tutor mailing list