[Tutor] Need help appending data to a logfile
Dave Angel
davea at davea.name
Thu Jun 27 17:55:46 CEST 2013
On 06/27/2013 10:55 AM, Matt D wrote:
> On 06/27/2013 10:36 AM, Matt D wrote:
>>
>>>
>>> You asked about a "save-as" feature. Why isn't that as simple as
>>> copying the current contents of the saved csv file? Or do you not know
>>> how you would go about copying?
>>>
>>
>> Hi. So I have the logger working, meaning the correct data is being
>> written to the 'internal' file 'logfile.txt' which is opened like this:
>>
>> self.logfile = open('logfile.txt', 'a')
>>
>> And I have the button that starts the file dialog like this:
>>
>> #open file dialog -----------------------------
>> 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)
>> with open(mypath, "a") as f:
>>
>>
>> Is there a simple way to copy what is in the 'internal' logfile.txt to
>> the file that the user chooses from the button?
>> Thanks!
shutil.copy(src, dst)
See: http://docs.python.org/2/library/shutil.html#shutil.copy
<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().
--
DaveA
More information about the Tutor
mailing list