Write to file and see content on screen

MRAB google at mrabarnett.plus.com
Tue Jun 10 05:08:09 EDT 2008


On Jun 10, 2:01 am, lama... at verizon.net wrote:
> Hello all,
>
> New user to python.   I can write to a file, however, I would like to
> do both...whatever I do on the screen, I'd like to write it to a file.
>
> any pointers on where I can find this info.
>
> thanks,

Something like this, perhaps?

class Storage(object):
    def __init__(self, the_file):
        self.the_file = the_file
    def write(self, message):
        import sys
        sys.stdout.write(message)
        self.the_file.write(message)

my_file = open("my_text.txt", "w")
store = Storage(my_file)
print >> store, "Hello world!"
my_file.close()



More information about the Python-list mailing list