f



write to log file

Hi,
I have a log file and of course I want to add the new information to the 
end of that log file. Unfortunately I always delete the old information 
and only append the current info.

Right now I do it like this:
I call a module which consists of a class and a function. It actually 
doesn't matter, but I just included it for the sake of completeness.
The module looks like this:

class log_C:
   def errorlog(self, filename, Data, d):
       LogFile = file(filename, 'w')
       ErrorInfo = Data[d+1]
       LogFile.write(ErrorInfo)
       LogFile.close()

How can I add the new info without deleting the old info?
I tried the modes 'a' and 'a+' but they both didn't work. Actually the 
data was totally unreadable! It was kind of messed up. I don't know why.

Thanks for your help.
Regards, Tom

0
9/29/2003 8:04:33 PM
comp.lang.python 77058 articles. 6 followers. Post Follow

2 Replies
487 Views

Similar Articles

[PageSpeed] 29

Tom wrote:
> Hi,
> I have a log file and of course I want to add the new information to the 
> end of that log file. Unfortunately I always delete the old information 
> and only append the current info.
> 
> Right now I do it like this:
> I call a module which consists of a class and a function. It actually 
> doesn't matter, but I just included it for the sake of completeness.
> The module looks like this:
> 
> class log_C:
>   def errorlog(self, filename, Data, d):
>       LogFile = file(filename, 'w')
>       ErrorInfo = Data[d+1]
>       LogFile.write(ErrorInfo)
>       LogFile.close()
> 
> How can I add the new info without deleting the old info?
> I tried the modes 'a' and 'a+' but they both didn't work. Actually the 
> data was totally unreadable! It was kind of messed up. I don't know why.

The following works for me:

 >>> f = open('c:/temp/test.txt', 'w')
 >>> for x in ('fee', 'fi', 'fum', 'fo'):
....     print >> f, x
....
 >>> f.close()
 >>> g = open('c:/temp/test.txt', 'a+r')
 >>> print >> g, 'slag-blah'
 >>> g.close()
 >>> file('c:/temp/test.txt').readlines()
['fee\n', 'fi\n', 'fum\n', 'fo\n', 'slag-blah\n']

HTH,

-- 
Hans (hans@zephyrfalcon.org)
http://zephyrfalcon.org/



0
hans4796 (117)
9/29/2003 8:43:45 PM
I wrote:

>  >>> g = open('c:/temp/test.txt', 'a+r')

Then again, using 'a' or 'a+' should work too...

-- 
Hans (hans@zephyrfalcon.org)
http://zephyrfalcon.org/



0
hans4796 (117)
9/29/2003 8:47:46 PM
Reply: