How to depress the output of an external module ?

Carl Banks pavlovevidence at gmail.com
Tue Dec 26 19:24:14 EST 2006


fdu.xiaojf at gmail.com wrote:
> After some trials I found that put "os.close(1)" before calling the
> function will depress the output. In fact, "os.close(1)" closed
> standard output, but I don't know how to open it again after the function's
> execution.

Try this:

fd = os.dup(1)
os.close(1)
sys.stdout = os.fdopen(fd)

It's poor design and extremely inconsiderate for a library to print to
stdout except as an overridable default.    (Not to mention it gives
ammo to people who want dynamically scoped globals.)  A long term
solution might be to haggle the author to stop being such a jerk.

Carl Banks




More information about the Python-list mailing list