saving the text on python dos window.

Meno dontaskme at doityourself.com
Thu Jun 24 20:40:43 CEST 2004


sarmin kho <sarmin_kho at yahoo.com> wrote in message news:<mailman.77.1088067779.27577.python-list at python.org>...
> Hi Pythoners..
>  
> "print (text)' command will print the 'text' on dos window when executing a python script. i would like to save all the text printed on the dos window to a file at the end of the python script execution..
>  

Hi Sarmin,

2 extra ideas.

First, you could just redirect the output when calling your python
script, something like: "python myScript.py > log.txt"

Or, you could also redirect the output inside your python scripts:

import sys
# save the current stdout, you should set it back to
# it's original value before the script execution ends
saveout = sys.stdout

# redirect the standard output to a file of your choice
fsock = open('out.log', 'w')
sys.stdout = fsock

# do something
print 'This message will be logged instead of displayed'

# revert back to standard stdout and close log file
sys.stdout = saveout
fsock.close()

(This example comes almost straight from the diveintopython tutorial
p137 - you can get it at http://diveintopython.org/. It is for sure an
interesting read)

Meno.



More information about the Python-list mailing list