Collect output to string
Terry Reedy
tjreedy at udel.edu
Tue Nov 23 18:22:41 EST 2010
On 11/23/2010 3:02 PM, Chris Rebert wrote:
> On Tue, Nov 23, 2010 at 11:53 AM, Burton Samograd<burton at userful.com> wrote:
>> Hello,
>>
>> I was wondering if there is any way in python to 'collect output to
>> string' as in some lisps/schemes. Output being, printed output to the
>> console using print.
>
> Rebind sys.stdout to a StringIO object.
> http://docs.python.org/library/sys.html#sys.stdout
> http://docs.python.org/library/stringio.html
If you are using print or print(), you can redirect output to the
StringIO object with >>sfile or file=sfile. I use the latter in a custom
test function where I normally want output to the screen but
occasionally want to capture test reports.
--
Terry Jan Reedy
More information about the Python-list
mailing list