473,385 Members | 1,798 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

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 2239
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Florian Wellner | last post by:
Hi, I've got a Problem when I want to save a .py file in Idle. Most times it works, but sometimes I can't save my file. In the "Python Shell" Window, I get the message: Exception in Tkinter...
11
by: Ed Suominen | last post by:
I'm thinking of implementing a real-time collaborative text editor in Python using Twisted. An initial plan is to use a Twisted PB server daemon that accepts user:password:file connections from...
16
by: Chris Maloof | last post by:
Hello, Does anyone know how I can read the ASCII text from a console window (from another application) in WinXP? It doesn't sound like a major operation, but although I can find the window via...
7
by: Brian Wallis | last post by:
This may be a FAQ,but I cannot find it. I want to save user preferences, window sizes, recently opened file names, etc for a python application and I am looking for a package that does this in a...
1
by: akermisch | last post by:
I have an SDI application with two splitter panes. The application is designed to open a text file and the user then can utilize a set of tools to modify the text. The modified text appears in a...
1
by: JohnJohnUSA | last post by:
I am using Python IDLE 2.4.3 on Windows XP. I use File->New Window to create a new program. In the Save As dialog, it always takes me to the python program directory (where python is installed)....
7
by: Dick Moores | last post by:
I accidentally stumbled across the Turtle Graphics module (turtle.py) the other day and have been having some fun with it. Now I'm wondering if there is a way to build into a script the saving...
9
by: Tyler | last post by:
Hello All: I am currently working on a project to create an FEM model for school. I was thinking about using wxPython to gather the 12 input variables from the user, then, after pressing the...
14
by: Stodge | last post by:
I'm trying to do the following. I have a Python application that is run: python app1.py --location=c:\test1 What I want to do is save the location parameter, so I can then do (in the same...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.