print to screen and file with one print statement 
Author Message
 print to screen and file with one print statement

I would like to send my print statements to the terminal and to a log
file at the same time. I can use redirect as describe in Dive into
Python:

import sys

print 'Dive in'                                          
saveout = sys.stdout                                    
fsock = open('out.log', 'w')
sys.stdout = fsock                                
print 'This message will be logged but not displayed'
print 'This message should be logged and also displayed'
sys.stdout = saveout                                    
fsock.close()    

This redirects all output from python to the open file.
At the same time I'd like to see all printed text on screen.
How can I do this?

Thanks

Mike



Mon, 01 Aug 2005 04:46:32 GMT  
 print to screen and file with one print statement
How about this:

class writer :
        def __init__(self, *writers) :
                self.writers = writers

        def write(self, text) :
                for w in self.writers :
                        w.write(text)

import sys

saved = sys.stdout
fout = file('out.log', 'w')
sys.stdout = writer(sys.stdout, fout)
print "There you go."
sys.stdout = saved
fout.close()

Quote:

> I would like to send my print statements to the terminal and to a log
> file at the same time. I can use redirect as describe in Dive into
> Python:

> import sys

> print 'Dive in'                                          
> saveout = sys.stdout                                    
> fsock = open('out.log', 'w')
> sys.stdout = fsock                                
> print 'This message will be logged but not displayed'
> print 'This message should be logged and also displayed'
> sys.stdout = saveout                                    
> fsock.close()    

> This redirects all output from Python to the open file.
> At the same time I'd like to see all printed text on screen.
> How can I do this?

> Thanks

> Mike



Mon, 01 Aug 2005 05:34:46 GMT  
 print to screen and file with one print statement
[Mike Mller]

Quote:
> This redirects all output from Python to the open file.
> At the same time I'd like to see all printed text on screen.
> How can I do this?

Same basic idea:

#!/usr/bin/env python

import sys

class MyWriter:

    def __init__(self, stdout, filename):
        self.stdout = stdout
        self.logfile = file(filename, 'a')

    def write(self, text):
        self.stdout.write(text)
        self.logfile.write(text)

    def close(self):
        self.stdout.close()
        self.logfile.close()

writer = MyWriter(sys.stdout, 'log.txt')
sys.stdout = writer

print 'test'

Cheers,

// m

-



Mon, 01 Aug 2005 05:24:06 GMT  
 print to screen and file with one print statement

Quote:

> Same basic idea:

> #!/usr/bin/env python

> import sys

> class MyWriter:

>     def __init__(self, stdout, filename):
>         self.stdout = stdout
>         self.logfile = file(filename, 'a')

>     def write(self, text):
>         self.stdout.write(text)
>         self.logfile.write(text)

>     def close(self):
>         self.stdout.close()
>         self.logfile.close()

> writer = MyWriter(sys.stdout, 'log.txt')
> sys.stdout = writer

> print 'test'

Hi Mark,

works perfect. Just had to change file to open for my Python 2.1.
Adding the method flush(self) to Writer helped to get my
sys.stdout.flush() to work.

Thanks also to two inches (previous post).

Mike



Mon, 01 Aug 2005 20:51:02 GMT  
 
 [ 4 post ] 

 Relevant Pages 

1. Writing to screen and log file in one statement

2. how to read one file, print to two files

3. print multiple files from one source file

4. how print output to screen and output file

5. PRINT PREVIEWER FREEWARE - FormView - Views HP PCL Print Output / Prints PCL to ANY WINDOWS PRINTER

6. reading from a file and printing it to the screen

7. LPT1 Printer Spooling/Printing and Appending to a Print File

8. Report - no one answered - Print to File prob

9. printing 2 unrelated files on one page

10. how to print two file in one time ??

11. Problem mixing print_exc and print in one file stream

12. Gawk 3.1.1 - Question regarding a variable in a print statement and help with code

 

 
Powered by phpBB® Forum Software