Account Options

  1. Sign in
The old Google Groups will be going away soon, but your browser is incompatible with the new version.
Google Groups Home
« Groups Home
print to screen and file with one print statement
There are currently too many topics in this group that display first. To make this topic appear first, remove this option from another topic.
There was an error processing your request. Please try again.
flag
  4 messages - Collapse all  -  Translate all to Translated (View all originals)
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
 
From:
To:
Cc:
Followup To:
Add Cc | Add Followup-to | Edit Subject
Subject:
Validation:
For verification purposes please type the characters you see in the picture below or the numbers you hear by clicking the accessibility icon. Listen and type the numbers you hear
 
Mike Müller  
View profile  
 More options Feb 12 2003, 3:46 pm
Newsgroups: comp.lang.python
From: mmuel...@dgfz.de (Mike Müller)
Date: 12 Feb 2003 12:46:32 -0800
Local: Wed, Feb 12 2003 3:46 pm
Subject: 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


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Two Inches  
View profile  
 More options Feb 12 2003, 4:35 pm
Newsgroups: comp.lang.python
From: Two Inches <missedby2inc...@gmx.net>
Date: Wed, 12 Feb 2003 22:34:46 +0100
Local: Wed, Feb 12 2003 4:34 pm
Subject: Re: 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()


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Mark McEahern  
View profile  
 More options Feb 12 2003, 6:01 pm
Newsgroups: comp.lang.python
From: "Mark McEahern" <markli...@mceahern.com>
Date: Wed, 12 Feb 2003 15:24:06 -0600
Local: Wed, Feb 12 2003 4:24 pm
Subject: RE: print to screen and file with one print statement
[Mike Müller]

> 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

-


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Mike Müller  
View profile  
 More options Feb 13 2003, 7:51 am
Newsgroups: comp.lang.python
From: mmuel...@dgfz.de (Mike Müller)
Date: 13 Feb 2003 04:51:02 -0800
Local: Thurs, Feb 13 2003 7:51 am
Subject: Re: print to screen and file with one print statement

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


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
End of messages
« Back to Discussions « Newer topic     Older topic »