saving the text on python dos window.

S

sarmin kho

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..

sys.stdout.GetLine() gave me an empty string..
where is the handle to the printed text, please?

i m also using scripts written by someone else so some printed texts are not mine but i would like to keep all the printed text for debugging.. the printed texts contain not only error messages but also all other texts i wish to keep.

many thanks chaps..

sarmin
 
D

Daniel Dittmar

sarmin said:
"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..

1. Create a stream class that writes anything both to sys.stdout and to a
file
2. replace sys.stdout with an instance of this class

class TeeStream:
def __init__ (self, *outstreams):
self.outstreams = outstreams

def write (self, text):
for outstream in self.outstreams:
outstream.write (text)

# do the same for writelines, flush etc.

import sys
mylogfile = open ('mylogfile', 'w')
sys.stdout = TeeStream (mylogfile, sys.stdout)
sys.stderr = TeeStream (mylogfile, sys.stderr)

Daniel
 
P

Peter Otten

Daniel said:
class TeeStream:
def __init__ (self, *outstreams):
self.outstreams = outstreams

def write (self, text):
for outstream in self.outstreams:
outstream.write (text)
Nice.

# do the same for writelines, flush etc.

Or if you are really lazy, use this modification:

class Tee:
def __init__ (self, *instances):
self._instances = instances

def __getattr__(self, name):

def call(self, *args, **kw):
for i in self._instances:
getattr(i, name)(*args, **kw)

setattr(self.__class__, name, call)
return getattr(self, name)

It creates methods lazily on the fly as needed.

Peter
 
L

Larry Bates

You may want to take a look at the logger class
that is built into python. You can create a log
of everything that goes to the screen for every
session that the program runs. It also provides
a powerful way to log any errors/exceptions that
may occur during program execution.

HTH,
Larry Bates
Syscon, Inc.

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..

sys.stdout.GetLine() gave me an empty string..
where is the handle to the printed text, please?

i m also using scripts written by someone else so some printed texts are not
mine but i would like to keep all the printed text for debugging.. the
printed texts contain not only error messages but also all other texts i
wish to keep.

many thanks chaps..

sarmin


Do you Yahoo!?
Yahoo! Mail - 50x more storage than other providers!
 
M

Meno

sarmin kho said:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,266
Messages
2,571,079
Members
48,772
Latest member
Backspace Studios

Latest Threads

Top