[Tutor] Writing logfile data to a user opened file
Prasad, Ramit
ramit.prasad at jpmorgan.com
Fri Jun 21 17:52:28 CEST 2013
Matt D wrote:
> Hey guys!
> So now my UI panel works well with this:
>
> # Open file button click event binded to openfile
> btn = wx.Button(self, -1, "Click me")
> sizer.Add(btn, pos=(7,2))
> btn.Bind(wx.EVT_BUTTON, self.openFile)
>
> #set the panel layout
> self.SetSizer(sizer)
> #makes the gui system fit all the controls onto the panel7
> self.Layout()
> self.Fit()
>
> EVT_DATA_EVENT(self, self.display_data)
> #self.watcher = traffic_watcher_thread(self.msgq, self)
>
> # openfile defined to start FileDialog
> def openFile(self, evt):
> with wx.FileDialog(self, "Choose a file", os.getcwd(), "",
> "*.*", wx.OPEN) as dlg:
> if dlg.ShowModal() == wx.ID_OK:
> path = dlg.GetPath()
> mypath = os.path.basename(path)
>
>
> So as you guys taught me to do I was opening 'logfile' this way:
>
> self.logfile = open('logfile.txt', 'a')
>
The point of using append mode is that you can close the file
and then re-open it.
> And the logger code:
>
> #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()))))
> # 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)))
> #end logger code ----------------
# first new line
with open( filename, 'a' ) as f:
#self.logfile.write('\n') # Just add this directly to the end of each line
# date and time
self.logfile.write('%s,'%( 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)))
self.logfile.write('\n')
# File is now automatically closed
Although, I would be tempted to just use the csv module (untested
in append mode) and list context.
#import csv (at the top of the module)
with open( filename, 'ab' ) as f: # open as binary to avoid blank rows
writer = csv.writer(f)
data = [ str(field_values.get(key)) for key in sorted(self.fields)
if field_values.get(key) ] # keep fields in same order
# insert date to beginning
data.insert(0, strftime("%Y-%m-%d %H:%M:%S", localtime()))
writer.writerow(data)
# now file is automatically closed
>
> And that is working well.
> Now I am trying to think of a way to get what is in the logfile.txt into
> the file that is opened by the user via the UI. Getting stuck trying to
> come up with an idea!? maybe something in python like 'user_opened_file
> = logfile' or 'write logfile to user_opened_file'? I am not able to
> find standard way to do this.
> Cheers!
>
> --
> Matt D
> ------------
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
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