Help me to print to screen as well as log

Peter Otten __peter__ at
Fri Nov 22 15:26:31 CET 2013

Himanshu Garg wrote:

> I want that print "hello" should appear on screen as well as get saved in
> a log file.
> How can I accomplish this?

In Python 3 print() is a function -- you can replace it with a custom 
function that invokes the original print() twice.

In both Python 3 and Python 2 you can redirect sys.stdout/stderr to a custom 
object with a write() method.

If you are actually logging I suggest that you look into the logging package 
which allows multiple handlers, see

You will need to replace your print() calls with

or similar.

More information about the Python-list mailing list