FW: Printing to a file and a terminal at the same time

Michael Torrie torriem at gmail.com
Tue Oct 10 23:42:16 EDT 2017


On 10/10/2017 10:09 AM, Vail, Rick wrote:
> I have a script for Cisco devices that will do configuration or any CLI command.  What I would like to do is print the output to my terminal(windows) and to a file. I can come up with stdout parameters
> To print to a file but not to the screen and when I remove the stdout part it prints to the screen only.
> 
> This prints to a file
> filename  = open("outputfile",'w')
> sys.stdout = filename
> print ("Anything printed will go to the output file")

Is this script interactive?  I'm guessing not. In any case, the
recommended course for any Unix OS is to just use the tee command to
redirect the output both to the terminal and to a file.  Works with
anything that outputs text to standard out.

As for doing it in Python, maybe you can implement your own print-like
function that prints everything out twice. Once to the file and once to
std out.






More information about the Python-list mailing list