Need your help

Chris Rebert clp2 at rebertia.com
Thu Apr 28 13:14:28 CEST 2011


On Wed, Apr 27, 2011 at 10:38 PM, 1011_wxy <1011_wxy at 163.com> wrote:
> Hi friends:
>
> 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!

import a, b, sys
def c():
    orig_stdout = sys.stdout
    sys.stdout = open('my_log_file.log', 'w')
    a.a()
    b.b()
    sys.stdout.close()
    sys.stdout = orig_stdout


Someone may have written a with-statement context manager that
abstracts away the swapping.

Cheers,
Chris
--
http://rebertia.com



More information about the Python-list mailing list