[Tutor] Need help appending data to a logfile
Dave Angel
davea at davea.name
Thu Jun 27 23:45:10 CEST 2013
On 06/27/2013 05:09 PM, Matt D wrote:
> On 06/27/2013 12:54 PM, Dave Angel wrote:
>> On 06/27/2013 12:33 PM, Matt D wrote:
>>>
>>>>
>>>> <SNIP>
>>>>> I forgot to mention i have the 'with open(mypath, "a") as f: commented
>>>>> out because it was making an indentation error that i could not fix.
>>>>
>>>> It was indented, and should not have been. The extra indentation
>>>> FOLLOWS the with statement, it's not correct to indent the line itself.
>>>>
>>>> Line it up with mypath=
>>>>
>>>> Isn't the rule for indentation simple enough? If a line ends with a
>>>> colon, you indent the next line. And keep indenting till the scope of
>>>> that colon ends. I don't know of any exceptions, but if there are any,
>>>> they're rare. Anyway, when the compiler complains, there aren't many
>>>> choices on how to change the line, so experimentation should teach you
>>>> pretty quickly.
>>>>
>>>> Maybe you're getting confused about indentation because your editor
>>>> doesn't handle tabs correctly. If you mix tabs and spaces, you'll drive
>>>> yourself crazy, at least with 2.x Python 3 tells you about it with a
>>>> new error. The easy answer (and my strong preference) is to never use
>>>> tabs in Python sources. Expand all your existing tabs (to 4 column
>>>> intervals), and tell your editor to always expand the tab key when
>>>> entering.
>>>>
>>>> I suggest you fix this problem first, and understand the fix, before
>>>> going off and using shutil.copy().
>>>>
>>>>
>>> Thanks! I do have 'with open(mypath, "a") as f:' lined up with the
>>> mypath line above (not sure why it indented with the paste to email) and
>>> I already replaced all tabs with spaces. seriously there is not one
>>> single tab in the whole program. but when that line is not commented out
>>> the program bails with the indentation error, other wise its fine. .
>>> So i don't know what i can do about that.
>>> Thanks!
>>
>> What text editor are you using? Can you literally move the cursor one
>> column at a time and see what column each of those two lines are in?
>> It's a little hard to imagine your email program converting all those
>> spaces to tabs.
>>
>> If it were my file, and I got to this point, I'd be using a dump program
>> to look at the actual file. Or I'd configure my text editor for
>> 40-column tabs, to make them very visible and very obnoxious.
>>
>> In Linux, use tweak, or any of dozens of others. Likewise there are
>> lots in Windows -- no idea what one to suggest.
>>
> 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.
So you're saying that the exception was on the following line? Why
didn't you say so? I just assumed you had clipped the remaining lines
of the function when you posted it.
So, the way to get rid of this new indentation error is to add a pass
statement inside the with clause. Indent it by 4 columns.
If that works, then you'll know how to deal with it next time.
As for the shutil.copy() function, how complex can it be? It takes two
file names, source and destination. It does not need a with statement
since it wants strings, not file handles.
You might get into trouble on some OS's with the source file already
open, but since you open it with append, it should be trivial to close
and reopen it.
--
DaveA
More information about the Tutor
mailing list