By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,296 Members | 1,476 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,296 IT Pros & Developers. It's quick & easy.

redirecting stdout to a file as well as screen

P: n/a
How could i make, from inside the program, to have the stdout and
stderr to be printed both to a file as well the terminal(as usual).

Apr 12 '07 #1
Share this Question
Share on Google+
10 Replies


P: n/a
Ant
On Apr 12, 8:14 am, "SamG" <mad.vi...@gmail.comwrote:
How could i make, from inside the program, to have the stdout and
stderr to be printed both to a file as well the terminal(as usual).
One way would be to create a custom class which has the same methods
as the file type, and held a list of file-like objects to write to.
e.g.

class multicaster(object):
def __init__(self, filelist):
self.filelist = filelist

def write(self, str):
for f in self.filelist:
f.write(str)
def writelines(self, str_list):
#etc

Then assign stdout and stderr to a new instance of one of these
objects:

mc = multicaster([sys.stdout, sys.stderr, log_file])
sys.stdout = mc
sys.stderr = mc

HTH
Apr 12 '07 #2

P: n/a
En Thu, 12 Apr 2007 04:14:32 -0300, SamG <ma*******@gmail.comescribió:
How could i make, from inside the program, to have the stdout and
stderr to be printed both to a file as well the terminal(as usual).
A very minimal example:

import sys

class Tee(file):
others = ()

def write(self, data):
file.write(self, data)
for f in others:
f.write(data)

tee = Tee(r"c:\temp\output.log","wt")
tee.others = [sys.stdout, sys.stderr]
sys.stdout = sys.stderr = tee

print dir(sys)
sys.foo # error

--
Gabriel Genellina

Apr 12 '07 #3

P: n/a
On Apr 12, 1:00 pm, "Gabriel Genellina" <gagsl-...@yahoo.com.ar>
wrote:
En Thu, 12 Apr 2007 04:14:32 -0300, SamG <mad.vi...@gmail.comescribió:
How could i make, from inside the program, to have the stdout and
stderr to be printed both to a file as well the terminal(as usual).

A very minimal example:

import sys

class Tee(file):
others = ()

def write(self, data):
file.write(self, data)
for f in others:
f.write(data)

tee = Tee(r"c:\temp\output.log","wt")
tee.others = [sys.stdout, sys.stderr]
sys.stdout = sys.stderr = tee

print dir(sys)
sys.foo # error

--
Gabriel Genellina
This is only creating an out.log file and all the stdout and stderr
are logged there.

Apr 12 '07 #4

P: n/a
On Apr 12, 12:40 pm, "Ant" <ant...@gmail.comwrote:
On Apr 12, 8:14 am, "SamG" <mad.vi...@gmail.comwrote:
How could i make, from inside the program, to have the stdout and
stderr to be printed both to a file as well the terminal(as usual).

One way would be to create a custom class which has the same methods
as the file type, and held a list of file-like objects to write to.
e.g.

class multicaster(object):
def __init__(self, filelist):
self.filelist = filelist

def write(self, str):
for f in self.filelist:
f.write(str)
def writelines(self, str_list):
#etc

Then assign stdout and stderr to a new instance of one of these
objects:

mc = multicaster([sys.stdout, sys.stderr, log_file])
sys.stdout = mc
sys.stderr = mc

HTH


I have written this....

import sys

class multicaster(object):
def __init__(self, filelist):
self.filelist = filelist

def write(self, str):
for f in self.filelist:
f.write(str)

log_file='out.log'
mc = multicaster([sys.stdout, sys.stderr, log_file])
sys.stdout = mc
sys.stderr = mc

print "Hello"

And i get this when i run the porgram.

HelloHelloTraceback (most recent call last):
Traceback (most recent call last):

Kindly advice!
Apr 12 '07 #5

P: n/a
On Apr 12, 3:35 am, "SamG" <mad.vi...@gmail.comwrote:
On Apr 12, 12:40 pm, "Ant" <ant...@gmail.comwrote:
On Apr 12, 8:14 am, "SamG" <mad.vi...@gmail.comwrote:
How could i make, from inside the program, to have the stdout and
stderr to be printed both to a file as well the terminal(as usual).
One way would be to create a custom class which has the same methods
as the file type, and held a list of file-like objects to write to.
e.g.
class multicaster(object):
def __init__(self, filelist):
self.filelist = filelist
def write(self, str):
for f in self.filelist:
f.write(str)
def writelines(self, str_list):
#etc
Then assign stdout and stderr to a new instance of one of these
objects:
mc = multicaster([sys.stdout, sys.stderr, log_file])
sys.stdout = mc
sys.stderr = mc
HTH

I have written this....

import sys

class multicaster(object):
def __init__(self, filelist):
self.filelist = filelist

def write(self, str):
for f in self.filelist:
f.write(str)

log_file='out.log'
mc = multicaster([sys.stdout, sys.stderr, log_file])
sys.stdout = mc
sys.stderr = mc

print "Hello"

And i get this when i run the porgram.

HelloHelloTraceback (most recent call last):
Traceback (most recent call last):

Kindly advice!
Try:

log_file = open("out.log", "w")

Apr 12 '07 #6

P: n/a
On 2007-04-12, SamG <ma*******@gmail.comwrote:
On Apr 12, 12:40 pm, "Ant" <ant...@gmail.comwrote:
>On Apr 12, 8:14 am, "SamG" <mad.vi...@gmail.comwrote:
How could i make, from inside the program, to have the stdout and
stderr to be printed both to a file as well the terminal(as usual).

One way would be to create a custom class which has the same methods
as the file type, and held a list of file-like objects to write to.
e.g.

class multicaster(object):
def __init__(self, filelist):
self.filelist = filelist

def write(self, str):
for f in self.filelist:
f.write(str)
def writelines(self, str_list):
#etc

Then assign stdout and stderr to a new instance of one of these
objects:

mc = multicaster([sys.stdout, sys.stderr, log_file])
sys.stdout = mc
sys.stderr = mc

HTH

I have written this....

import sys

class multicaster(object):
def __init__(self, filelist):
self.filelist = filelist

def write(self, str):
for f in self.filelist:
f.write(str)

log_file='out.log'
log_file is not a file but a string, So when you reach the
write method in your multicaster you get an atttribute error
because a string has no write method
mc = multicaster([sys.stdout, sys.stderr, log_file])
Since sys.stdout and sys.stderr usually both refer to
the terminal, this will result in your output appearing
twice on the terminal
sys.stdout = mc
sys.stderr = mc
Maybe you are better of leaving sys.stderr as it is,
at least until you are sure your multicaster itself
is working as it should.
print "Hello"
Apr 12 '07 #7

P: n/a
En Thu, 12 Apr 2007 06:01:18 -0300, SamG <ma*******@gmail.comescribió:
On Apr 12, 1:00 pm, "Gabriel Genellina" <gagsl-...@yahoo.com.ar>
wrote:
>En Thu, 12 Apr 2007 04:14:32 -0300, SamG <mad.vi...@gmail.comescribió:
How could i make, from inside the program, to have the stdout and
stderr to be printed both to a file as well the terminal(as usual).

class Tee(file):
others = ()

def write(self, data):
file.write(self, data)
for f in others:
f.write(data)

This is only creating an out.log file and all the stdout and stderr
are logged there.
Sorry, `for f in others:` should read `for f in self.others:`

--
Gabriel Genellina

Apr 12 '07 #8

P: n/a
On Apr 12, 3:16 pm, "Gabriel Genellina" <gagsl-...@yahoo.com.ar>
wrote:
En Thu, 12 Apr 2007 06:01:18 -0300, SamG <mad.vi...@gmail.comescribió:
On Apr 12, 1:00 pm, "Gabriel Genellina" <gagsl-...@yahoo.com.ar>
wrote:
En Thu, 12 Apr 2007 04:14:32 -0300, SamG <mad.vi...@gmail.comescribió:
How could i make, from inside the program, to have the stdout and
stderr to be printed both to a file as well the terminal(as usual).
class Tee(file):
others = ()
def write(self, data):
file.write(self, data)
for f in others:
f.write(data)
This is only creating an out.log file and all the stdout and stderr
are logged there.

Sorry, `for f in others:` should read `for f in self.others:`

--
Gabriel Genellina

Does not make difference, does this work for you? Im working on linux.
But this code looks portable except for the file path :)

Apr 12 '07 #9

P: n/a
En Thu, 12 Apr 2007 07:23:43 -0300, SamG <ma*******@gmail.comescribió:
How could i make, from inside the program, to have the stdout and
stderr to be printed both to a file as well the terminal(as usual).
>class Tee(file):
others = ()
> def write(self, data):
file.write(self, data)
for f in others:
f.write(data)
This is only creating an out.log file and all the stdout and stderr
are logged there.

Sorry, `for f in others:` should read `for f in self.others:`
Does not make difference, does this work for you? Im working on linux.
But this code looks portable except for the file path :)
Yes. And it's rather similar to your other example... Omit sys.stderr as
Antoon Pardon suggested.

--
Gabriel Genellina

Apr 12 '07 #10

P: n/a
On Apr 12, 3:42 pm, "Gabriel Genellina" <gagsl-...@yahoo.com.ar>
wrote:
En Thu, 12 Apr 2007 07:23:43 -0300, SamG <mad.vi...@gmail.comescribió:
How could i make, from inside the program, to have the stdout and
stderr to be printed both to a file as well the terminal(as usual).
class Tee(file):
others = ()
def write(self, data):
file.write(self, data)
for f in others:
f.write(data)
This is only creating an out.log file and all the stdout and stderr
are logged there.
Sorry, `for f in others:` should read `for f in self.others:`
Does not make difference, does this work for you? Im working on linux.
But this code looks portable except for the file path :)

Yes. And it's rather similar to your other example... Omit sys.stderr as
Antoon Pardon suggested.

--
Gabriel Genellina

Thanks people i have got it working now...

with this program!

#END
import sys

class multicaster(object):
def __init__(self, filelist):
self.filelist = filelist

def write(self, str):
for f in self.filelist:
f.write(str)

log_file=open('out.log','w')
mc = multicaster([sys.stderr, log_file])
sys.stdout = mc
sys.stderr = mc
sys.stdout.write( "Mojozoox\n")
sys.stderr.write( "Hello\n")
#END

works perfect for my needs.

Apr 12 '07 #11

This discussion thread is closed

Replies have been disabled for this discussion.