Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > How do I redirect output to a file and to the console screen?

Thread Tools

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

Posts: n/a
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 (-:

Reply With Quote
Martin Miller
Posts: n/a
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
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:



Reply With Quote
Posts: n/a
Thank U.

Reply With Quote

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
Reading console output and writing to console C++ 5 03-04-2008 09:00 AM
How can I redirect Console.Writeline to Response.Output? =?Utf-8?B?QUFPTSBUaW0=?= ASP .Net 2 03-21-2007 07:14 PM
Basic Q - Response.Redirect, all redirect to first Response.Redirect statement Sal ASP .Net Web Controls 1 05-15-2004 03:46 PM
Redirect output from console to a file Nishi Bhonsle Java 6 01-18-2004 10:58 PM