[Tutor] Writing logfile data to a user opened file

Alan Gauld alan.gauld at btinternet.com
Sat Jun 22 09:47:40 CEST 2013


On 22/06/13 02:42, Matt D wrote:

>              if dlg.ShowModal() == wx.ID_OK:
>                  path = dlg.GetPath()
>                  mypath = os.path.basename(path)
>                  with open(mypath, "a") as f:
>                  f.writelines(self.log_array)
>
> so thats how i used what you said, "with open() as f:".  is this the
> right way to open the file?

You need to indent the writelines() call so its inside
the 'with' block.

                   with open(mypath, "a") as f:
                        f.writelines(self.log_array)

This means you don't need to call f.close() each time - which you were 
missing from your earlier code.

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



More information about the Tutor mailing list