[Tutor] How to write this to a file?
Kent Johnson
kent37 at tds.net
Thu Oct 6 01:32:52 CEST 2005
Dick Moores wrote:
> I have a script that writes it's output to a file. I also print the time with
>
> print "Time was %.4g seconds" % (timeEnd - timeStart)
>
> How could I also have the same output of the print expression, written to
> the file?
The formatting part of the print is just an expression with a string value, you can assign it to a variable and write it to your file. You might want to add a newline:
timeMsg = "Time was %.4g seconds\n" % (timeEnd - timeStart)
f.write(timeMsg)
If you have a lot of output that you want to put to the console and to a log file you might like to look at the logging module. A single line of logging can be written to multiple places by the module.
Kent
More information about the Tutor
mailing list