468,769 Members | 1,910 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

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

How do I redirect output to a file and to the console screen?

Hi there,

I'm doing some TELNET and FTP sessions with my scripts. I need to
redirect all the output (and not just what I print) to an output file,
but still be able to see the session in process on the console.

The Console screen is of less importance, so I could give it up, but
what I'm looking for is a way to see all the interaction with the
remote seesions on a file, that all the console data was redirected to
it from within the script file.

P.S. - the command "python script_file > output_file" is not from
within the file, so it won't work for me.

Thank you (-:

Dec 28 '05 #1
2 3333
The basic way to redirect output is to reassign a value to sys.stdout
-- something along these lines:

# redirect stdout to a disk file
import sys
saveout = sys.stdout
outfile = open('output.txt', 'w')
sys.stdout = outfile

# output stuff
print 'hello world'

# restore stdout
outfile.flush()
outfile.close()
sys.stdout = saveout:

Essentially what you want is to have output redirected to *two*
different streams at the same time, the original stdout and a file.
Here's a link to a (very old) post on the subject that should help (see
'class Tee') if coupled with the above:
http://groups.google.com/group/comp....b52448c1cbc10e


-Martin

Dec 28 '05 #2
Thank U.

Dec 29 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

4 posts views Thread by rossum | last post: by
1 post views Thread by abcd | last post: by
10 posts views Thread by Jef Driesen | last post: by
1 post views Thread by =?Utf-8?B?cm9kY2hhcg==?= | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by Marin | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.