[Tutor] Need help appending data to a logfile

Alan Gauld alan.gauld at btinternet.com
Fri Jun 28 22:43:16 CEST 2013


On 28/06/13 20:54, Matt D wrote:

> 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:
>                  f.write(self.logfile)
>                  self.logfile.close()

It's not clear what state self.logfile is in but assuming you have been 
writing to it the cursor will be at the end of the file so you can't 
write anything from it.  You would need to do a seek(0) first.
But f.write(self.logfile) will not write the contents of logfile.
You will need to read that first so I think you need:

               with open(mypath, "a") as f:
                   self.logfile.seek(0)  # go to beginning of the file
                   f.write(self.logfile.read())  # write the contents
                   self.logfile.close()  # close but do not delete

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!)

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



More information about the Tutor mailing list