Need your help

Ervin Hegedüs airween at gmail.com
Thu Apr 28 07:20:16 EDT 2011


hello,
> Here I need some help.
>> #encoding="utf-8"
> #moudle a.py
> def a():
> print " function a!"
>> #encoding="utf-8"
> #moudle b.py
> def b():
> print " function b!"
>>> #encoding="utf-8"
> #moudle c.py
> import a
> import b
> def c():
> a.a()
> b.b()
>>> Here in function c,How can i record all the information printed by a and b with out modifying moudle a and b?
> I want to output all the printed information into a text file.
>> Need your help, thanks a lot!

sounds you want something like this:
#!/usr/bin/python
import a
import b
import sys
import StringIO
output = StringIO.StringIO()
def c():
 # save default stdout
 tout = sys.stdout
 # redirect stdout to StringIO object
 sys.stdout = output
 # a.a() prints their output to StringIO object
 a.a()
 # back up default stdout
 sys.stdout = tout
 # print StringIO object value
 print "retval:", output.getvalue()
c()
a.
 


More information about the Python-list mailing list

AltStyle によって変換されたページ (->オリジナル) /