return to the original output stream (as shown in the section on redirecting streams to objectsfor exampleimport sys print('spam file=sys stderrwill send text the standard error stream object rather than sys stdout for the duration of this single print call only the next normal print statement (without fileprints to standard output as usual similarlywe can use either our custom class or the standard library' class as the output file with this hookfrom io import stringio buff stringio(print( file=buffprint('spam'file=buffprint(buff getvalue() spam from redirect import output buff output(print( file=buffprint('eggs'file=buffprint(buff text eggs other redirection optionsos popen and subprocess revisited near the end of the preceding we took first look at the built-in os popen function and its subprocess popen relativewhich provide way to redirect another command' streams from within python program as we sawthese tools can be used to run shell command line ( string we would normally type at dos or csh promptbut also provide python file-like object connected to the command' output stream-reading the file object allows script to read another program' output suggested that these tools may be used to tap into input streams as well because of thatthe os popen and subprocess tools are another way to redirect streams of spawned programs and are close cousins to some of the techniques we just met their effect is much like the shell command-line pipe syntax for redirecting streams to programs (in facttheir names mean "pipe open")but they are run within script and provide file-like interface to piped streams they are similar in spirit to the redirect functionbut are based on running programs (not calling functions)and the command' streams are processed in the spawning script as files (not tied to class objectsthese tools redirect the streams of program that script startsinstead of redirecting the streams of the script itself script execution context