Hello,

Stephen Horne steve at lurking.demon.co.uk
Tue Jul 24 03:42:28 EDT 2001


On 22 Jul 2001 20:06:34 GMT, quinn at yak.ugcs.caltech.edu (Quinn Dunkan)
wrote:

>On Sun, 22 Jul 2001 06:43:18 +0500, Tom Bryan <tbryan at python.net> wrote:
>>Alex wrote:
>>
>>> Not really.  There are some ways you could speed up the python code:
>>
>>I'm also not really sure why one would want to do this, but...
>> 
>>> There's the additional problem that
>>> opening "err" will truncate the log file, and if an error occurs while
>>> you're writing the new version back, you could lose messages.
>>
>>here's a version that makes a copy of the file before prepending a 
>>new message to avoid that problem.  It also uses seek to avoid having 
>
>I suggest appending the normal way, but viewing it with 'tac' :)

Another possible alternative...

In initialisation, read the main file, invert the order of the lines
and save to a second temporary file...

  f = open ("file.log", "r")
  text = f.readlines ().reverse ()

  f = open ("temp.log", "w")
  for i in text :
    f.write (i)


While the program runs, log at the end of the temporary log file. Then
on completion, reverse what you did at the start...

  f = open ("temp.log", "r")
  text = f.readlines ().reverse ()

  f = open ("file.log", "w")
  for i in text :
    f.write (i)


If the program ran for a while, this might be the better approach.


You could also use the same general approach to print a normal-order
log file in last-error-first order - remember to slice off the newline
character at the end of each line before calling print though.


Another approach is to save all your errors into a list of strings in
memory and dump them out to err at the end - a risky one, though, if
you get a bug that stops the log getting saved.




More information about the Python-list mailing list