[Tutor] How to write this to a file?

Hans Dushanthakumar Hans.Dushanthakumar at navman.com
Thu Oct 6 22:37:22 CEST 2005

 print outputs the string to the std output (sys.stdout). You could
redirect the std output to a file instead of the screen as follows, but
it's a bit clumsy.

fptr = open("hans.log", "a+")
import sys
sys.stdout = fptr
print "Hi there"

This will output the string into the file hans.log. However, to get it
to print to the screen again, u will need to have stored the original
contents of sys.stdout somewhere and then redirected sys.stdout to that

TCL provides a more handy way of doing this:
print $fptr "Hi there"


-----Original Message-----
From: tutor-bounces at python.org [mailto:tutor-bounces at python.org] On
Behalf Of Dick Moores
Sent: Thursday, 6 October 2005 12:18 p.m.
To: tutor at python.org
Subject: [Tutor] How to write this to a file?

I have a script that writes it's output to a file. I also print the time

print "Time was %.4g seconds" % (timeEnd - timeStart)

How could I also have the same output of the print expression, written
to the file?


Dick Moores

Tutor maillist  -  Tutor at python.org

More information about the Tutor mailing list