Can we use print to print to more than sys.stdout?

Pierre_Rouleau at ImpathNetworks.com Pierre_Rouleau at ImpathNetworks.com
Tue Mar 12 14:49:45 EST 2002


Wow!  That's got to be why i like Python so much!
Elegant, powerful and great community support with superfast turn-around time to
get questions answered!!!
Hope it would be like that for some well know Companies...
Thanks !






"Jason Orendorff" <jason at jorendorff.com> on 03/12/2002 01:25:09 PM

To:   Pierre Rouleau/impath at impath, "python-list" <python-list at python.org>
cc:

Subject:  Re: Can we use print to print to more than sys.stdout?



Pierre Rouleau wrote:
> #1) Is it possible to change print behavior to make it print to several
> streams?
>
> #2)
> Is it also possible to define an object that would behave like a file
> and that would do the printing itself to wherever required?

# Sure.  This code does both.

class Mux:
    def __init__(self, *objects):
        self.__objects = objects
    def write(self, data):
        for obj in self.__objects:
            obj.write(data)
    def writelines(self, lines):
        for obj in self.__objects:
            obj.writelines(lines)

myfile = open('somefile.txt', 'a')
sys.stdout = Mux(sys.stdout, myfile)

## Jason Orendorff    http://www.jorendorff.com/










More information about the Python-list mailing list