Write both in std output and in a file ?

Daniel Dittmar daniel.dittmar at sap.com
Thu Jan 17 11:08:09 EST 2002


> Is there an easy way to implement, that a timestamp is also
> printed for each print that I call without having to specify it in the
> print statement itself?

Assuming that you want to print the timestamp for every line:

def write (self, data):
    timestamp = time.strftime (...)
    if self.lastWasNl:
        data = timestamp + data
    if data [-1] == '\n':
        data = data [:-1].replace ('\n', '\n' + timestamp) + '\n'
        self.lastWasNl = 1
    else:
        data = data.replace ('\n', '\n' + timestamp)
        self.lastWasNl = 0
    for stream in self.streams:
        stream.write (data)






More information about the Python-list mailing list