How to catch python's STDOUT

Fredrik Lundh fredrik at pythonware.com
Thu Apr 6 05:52:59 EDT 2006


praveenkumar.117 at gmail.com wrote:

>         I have python script in which i have some print statements.
>         I dont want the outputs of print to be displayed on the console
>         since it is used my fellow-workers
>         But i need those prints for debugging purpose
>         So some how i want to capture those prints
>         can u please suggest

you can print directly to a log file:

    mylogfile = open("logfile.txt", "a")

    print >>mylogfile, "my log message"

or you can replace sys.stdout (and/or sys.stderr) with the log file object:

    import sys
    sys.stdout = sys.stderr = open("logfile.txt", "a")

or you can use the "logging" module:

    http://docs.python.org/lib/module-logging.html

etc.

hope this helps!

</F>






More information about the Python-list mailing list