[Tutor] Need help appending data to a logfile

eryksun eryksun at gmail.com
Thu Jun 27 23:55:34 CEST 2013


On Thu, Jun 27, 2013 at 5:09 PM, Matt D <md123 at nycap.rr.com> wrote:
> I use gedit.  Yes sir, its all spaces, I'm not putting you on there are
> no tabs in my .py file. none.  I am pretty sure if i knew how to put the
> proper code after the 'with open()' thing, indented of course, that the
> indentation error would be gone.  But Ive been looking at the shutil
> thing and i can not figure out how to write the right code to go after
> the 'open with()' that will make whats in 'logfile.txt' get appended
> into whatever file the user opens.

The code you pasted earlier uses tabs, except for the last line:

    >>> print(src.replace('\t', 'tab>'))
    tab>def openFile(self, evt):
    tab>tab>with wx.FileDialog(self, "Choose a file", os.getcwd(), "",
    tab>                        "*.*", wx.OPEN) as dlg:
    tab>tab>tab>if dlg.ShowModal() == wx.ID_OK:
    tab>tab>tab>tab>path = dlg.GetPath()
    tab>tab>tab>tab>mypath = os.path.basename(path)
                                    with open(mypath, "a") as f:

Maybe tabs are being replaced with 8 spaces, while the rest of your
code uses 4-space indents. Note that the final "with open" is indented
by 32 spaces instead of 16:

    >>> src.splitlines()[-1].find('with')
    32

Just make sure you're indenting consistently. I use dotted indentation
guides and visible spaces/tabs. It makes the edit window noisy, but I
don't really notice it.


More information about the Tutor mailing list