print >> to a derived file

Scott David Daniels Scott.Daniels at Acm.Org
Tue Jan 15 20:45:31 EST 2008


iu2 wrote:
> Hi,
> 
> I'm trying to write data to both a file and the console, so I did:
> 
> class File_and_console(file):
> 	def write(self, s):
> 		file.write(self, s)
> 		print s,
 >>>> f = File_and_console('1.txt', 'w')
...

Always use the same method for writing, or you will get weird results.
If you think the above works, try:
          for char in 'sample': f.write(char)

class MultiWrite(object):

     def write(self, text):
         for dest in self._files:
             dest.write(text)

     def __init__(self, *files):
         self._files = files

     def close(self):
         for dest in self._files:
             dest.close()


f1 = MultiWrite(open('1.txt', 'w'), sys.stdout)

--Scott David Daniels
Scott.Daniels at Acm.Org




More information about the Python-list mailing list