Writing Log CSV (Efficiently)

skip at pobox.com skip at pobox.com
Mon Apr 16 16:11:24 CEST 2007

    Rob> I'm looking to write a Log file which will be CSV based, and there
    Rob> is a good possibility that it'll get quite busy once its up and
    Rob> running, so I'm looking for the most efficient way to achieve
    Rob> it.

In addition to Tim's advice, if you're worried about possible loss of data
you can set a timer to flush the file object every second or so.

    myfile = open("Logs/Application.txt", "wb")   # note "b"inary mode!
    writer = csv.writer(myfile)
    set_a_timer(1.0, myfile.flush)

You didn't indicate your platform so I can't be more specific about
set_a_timer().  In my work environment we use Gtk a lot, so
gobject.timeout_add works for us.  You could also call signal.alarm on
Unixoid systems.  I'm sure there's something similar in Win32.


More information about the Python-list mailing list