469,949 Members | 2,723 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,949 developers. It's quick & easy.

saving the text on python dos window.

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!
Jul 18 '05 #1
4 2145
sarmin kho wrote:
"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
Jul 18 '05 #2
Daniel Dittmar wrote:
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

Jul 18 '05 #3
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.

"sarmin kho" <sa********@yahoo.com> wrote in message
news:ma*************************************@pytho n.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..

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!
Jul 18 '05 #4
sarmin kho <sa********@yahoo.com> wrote in message news:<ma*************************************@pyth on.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.
Jul 18 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by Florian Wellner | last post: by
11 posts views Thread by Ed Suominen | last post: by
16 posts views Thread by Chris Maloof | last post: by
7 posts views Thread by Brian Wallis | last post: by
1 post views Thread by akermisch | last post: by
7 posts views Thread by Dick Moores | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.