Appending a log file and see progress as program executes

Matteo mahall at ncsa.uiuc.edu
Wed May 30 14:29:38 EDT 2007


On May 30, 1:03 pm, "Karim Ali" <kak... at hotmail.com> wrote:
> Hi,
>
> I am writing a program that will take several days to execute :) and would
> like to append to a log file but be able to open that file at any time and
> see the errors that have occured.
>
> So this is what I am doing:
>
> ----------------------------------------------
> flog = open('out.log', 'a')
> ....
> when needed:
> sys.stdout=flog
> print "error message"
> --------------------------------------------

I imagine that your problem is that stdout is buffered, and hence only
writes to the output when the buffer is full. To ameliorate this, you
can use flog.flush() after every print statement.
Other alternatives:
- Use stderr for printing log messages, and run python in unbuffered
mode (python -u script.py)
  You can store the log file by redirecting stderr. Using bash, this
would be:
  python -u script.py 2>out.log

- Write an autoflush class:
  class AutoFlush:
    def __init__(self, stream):
        self.stream = stream
    def write(self, text):
        self.stream.write(text)
        self.stream.flush()

  ...
  flog=open('out.log','a')
  flog=AutoFlush(flog)

  print >>flog,"I'm a message!"

Note that instead of changing stdout, you can also print directly to a
file with:
print >>flog,"Something to print"

cheers,
-matt




More information about the Python-list mailing list