[Tutor] Need help appending data to a logfile

Prasad, Ramit ramit.prasad at jpmorgan.com
Wed Jun 19 20:13:51 CEST 2013


Peter Otten wrote:
> Matt D wrote:
> 
> > Hey,
> > I wrote some simple code to write data to a logfile and it works pretty
> > well (thanks guys).  Now my problem is that every time i run the program
> > the old logfile.txt is overwritten.
> 
> The help() function in the interactive interpreter is a good tool hunt for
> help on features of functions and classes. For example:
> 
> >>> help(open)
> Help on built-in function open in module __builtin__:
> 
> open(...)
>     open(name[, mode[, buffering]]) -> file object
> 
>     Open a file using the file() type, returns a file object.  This is the
>     preferred way to open a file.  See file.__doc__ for further information.
> 
> >>> help(file)
> Help on class file in module __builtin__:
> 
> class file(object)
>  |  file(name[, mode[, buffering]]) -> file object
>  |
>  |  Open a file.  The mode can be 'r', 'w' or 'a' for reading (default),
>  |  writing or appending.  The file will be created if it doesn't exist
> [...]
> 
> 
> > I need to be able to stop and start
> > the program without overwriting, or losing, the old data.  here is the
> > relavent code:
> >
> >  #  central part of the program
> >  #  lays out the GUI panel
> >  #  omitted lots for our purposes here
> >  Class panel(wx.Panel):
> >
> >         #  open a file named "logfile.txt" in "w" writing mode.
> >         #  this will create the file if it doesn't exist.
> >         self.logfile = open('logfile.txt', 'w')
> >
> >  # Updates the TextCtrl field values
> >  # and logs TextCtrl field values
> >  def update(self, field_values):
> >
> >     #logger code---------------
> >         #first write the CURRENT date/time
> > self.logfile.write('%s,'%(str(strftime("%Y-%m-%d %H:%M:%S",
> > gmtime()))))
> > # 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)))
> > self.logfile.write('\n')
> > #end logger code ----------------
> >
> > In addition to not deleting the old data, it would be awesome to have
> > some sort of wxPython widget that would give the user the ability to
> > 'save as', or name and save the file, from the GUI panel.
> 
> This last request is a bit vague, and I'm not a wxPython user myself -- but
> the wx.FileSelector() function seems like a good start.
> 
> Unfortunately the documentation I found is for the underlying C++ library:
> 
> <http://docs.wxwidgets.org/stable/wx_dialogfunctions.html#wxfileselector>


I think he wanted:
http://www.wxpython.org/docs/api/wx.FileDialog-class.html 

> 
> May layman's translation into wxPython:
> 
> >>> import wx
> >>> app = wx.App()
> >>> wx.FileSelector("Save logfile as", flags=wx.FD_SAVE)
> [snip spurious warnings]
> u'/foo/bar.baz' # use that result for the actual saving.

If you want to do this, you need to run the wx.App() line only once per 
application run. You also need to store all the text that you were writing 
to file in a list because you cannot write to file and then decide the name
later. You must know location first and then write data. Data is not 
guaranteed to be written until the file is closed as it can stay in-memory
until a buffer is full and then gets written to file. Closing a file should
flush the buffer contents to file.

Dave Angel is correct. Look at the documentation for open() or just store 
the data in memory and only write when a user selects a save button/menu.


~Ramit


This email is confidential and subject to important disclaimers and
conditions including on offers for the purchase or sale of
securities, accuracy and completeness of information, viruses,
confidentiality, legal privilege, and legal entity disclaimers,
available at http://www.jpmorgan.com/pages/disclosures/email.  


More information about the Tutor mailing list