473,418 Members | 2,083 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,418 software developers and data experts.

Issue of redirecting the stdout to both file and screen

I wanna print the log to both the screen and file, so I simulatered a
'tee'

class Tee(file):

def __init__(self, name, mode):
file.__init__(self, name, mode)
self.stdout = sys.stdout
sys.stdout = self

def __del__(self):
sys.stdout = self.stdout
self.close()

def write(self, data):
file.write(self, data)
self.stdout.write(data)

Tee('logfile', 'w')
print >>sys.stdout, 'abcdefg'

I found that it only output to the file, nothing to screen. Why?
It seems the 'write' function was not called when I *print* something.

May 28 '07 #1
5 2586
人言落日是天涯,望极天涯不见家 wrote:
I wanna print the log to both the screen and file, so I simulatered a
'tee'

class Tee(file):

def __init__(self, name, mode):
file.__init__(self, name, mode)
self.stdout = sys.stdout
sys.stdout = self

def __del__(self):
sys.stdout = self.stdout
self.close()

def write(self, data):
file.write(self, data)
self.stdout.write(data)

Tee('logfile', 'w')
print >>sys.stdout, 'abcdefg'

I found that it only output to the file, nothing to screen. Why?
It seems the 'write' function was not called when I *print* something.
There are places in the C code of Python that do the equivalent of

if isinstance(file_like_object, file):
file.write(file_like_object, text)
else:
file_like_object.write(text)

Therefore you can't safely inherit from file. The workaround is to make your
own file-like object; yours would become

class Tee(object):
def __init__(self, name, mode):
self.file = open(name, mode)
self.stdout = sys.stdout
sys.stdout = self
def __del__(self):
sys.stdout = self.stdout
self.file.close()
def write(self, data):
self.file.write(data)
self.stdout.write(data)

Peter
May 28 '07 #2
En Mon, 28 May 2007 06:17:39 -0300, 人言落日是天涯,望极天涯不见家
<ke********@gmail.comescribió:
I wanna print the log to both the screen and file, so I simulatered a
'tee'

class Tee(file):

def __init__(self, name, mode):
file.__init__(self, name, mode)
self.stdout = sys.stdout
sys.stdout = self

def __del__(self):
sys.stdout = self.stdout
self.close()

def write(self, data):
file.write(self, data)
self.stdout.write(data)

Tee('logfile', 'w')
print >>sys.stdout, 'abcdefg'

I found that it only output to the file, nothing to screen. Why?
It seems the 'write' function was not called when I *print* something.
You create a Tee instance and it is immediately garbage collected. I'd
restore sys.stdout on Tee.close, not __del__ (you forgot to call the
inherited __del__ method, btw).
Mmm, doesn't work. I think there is an optimization somewhere: if it looks
like a real file object, it uses the original file write method, not yours.
The trick would be to use an object that does NOT inherit from file:

import sys
class TeeNoFile(object):
def __init__(self, name, mode):
self.file = open(name, mode)
self.stdout = sys.stdout
sys.stdout = self
def close(self):
if self.stdout is not None:
sys.stdout = self.stdout
self.stdout = None
if self.file is not None:
self.file.close()
self.file = None
def write(self, data):
self.file.write(data)
self.stdout.write(data)
def flush(self):
self.file.flush()
self.stdout.flush()
def __del__(self):
self.close()

tee=TeeNoFile('logfile', 'w')
print 'abcdefg'
print 'another line'
tee.close()
print 'screen only'
del tee # should do nothing

--
Gabriel Genellina

May 28 '07 #3
Gabriel Genellina wrote:
En Mon, 28 May 2007 06:17:39 -0300, ???????????????
<ke********@gmail.comescribi:
>I wanna print the log to both the screen and file, so I simulatered a
'tee'

class Tee(file):

def __init__(self, name, mode):
file.__init__(self, name, mode)
self.stdout = sys.stdout
sys.stdout = self

def __del__(self):
sys.stdout = self.stdout
self.close()

def write(self, data):
file.write(self, data)
self.stdout.write(data)

Tee('logfile', 'w')
print >>sys.stdout, 'abcdefg'

I found that it only output to the file, nothing to screen. Why?
It seems the 'write' function was not called when I *print* something.

You create a Tee instance and it is immediately garbage collected.
It is not garbage collected until the next assignment to sys.stdout.

Peter

May 28 '07 #4
En Mon, 28 May 2007 09:10:40 -0300, Peter Otten <__*******@web.de>
escribi:
Gabriel Genellina wrote:
>En Mon, 28 May 2007 06:17:39 -0300, ???????????????
<ke********@gmail.comescribi:
>> def __init__(self, name, mode):
file.__init__(self, name, mode)
self.stdout = sys.stdout
sys.stdout = self

Tee('logfile', 'w')
print >>sys.stdout, 'abcdefg'

I found that it only output to the file, nothing to screen. Why?
It seems the 'write' function was not called when I *print* something.

You create a Tee instance and it is immediately garbage collected.

It is not garbage collected until the next assignment to sys.stdout.
Yes, sorry, this was my first guess. Later I discovered the real reason
-you pointed it too-, I should have removed the whole first paragraph on
my reply.

--
Gabriel Genellina

May 28 '07 #5
I see. Many thanks to you!

May 29 '07 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Alexander Stippler | last post by:
Hi, The short story: I have to redirect stdout (not cout!!) to an ostream. How can I manage this? The long story: I use the NAGC numerics library with C++ and want to use its output routines...
2
by: Jason Heyes | last post by:
Here is what I use to redirect printed messages to a file: std::ofstream file("logfile"); std::cout.rdbuf(file.rdbuf()); std::cout << "this message goes to a file" << std::endl; Will the...
8
by: Morpheus | last post by:
I am trying to test a function that outputs text to the standard output, presumably using a cout call. I do not have access to the source, but need to test the output. Is this possible? I can...
13
by: souissipro | last post by:
Hi, I have written a C program that does some of the functionalities mentionned in my previous topic posted some days ago. This shell should: 1- execute input commands from standard input,...
1
by: pp4175 | last post by:
Hello Everyone, I am currently working on writing a GUI wrapper for a Menu driven DOS Program. What I was looking on doing is redirecting stdout/stdin to a stream and read/write similar to a...
10
by: SamG | last post by:
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).
3
by: pnsreee | last post by:
Hi All, I am trying to print a log file created in the same script with the code bellow. but Im not getting output on screen. The output file is created using rediretion of output. ...
0
by: Tom Gaudasinski | last post by:
Greetings, I'm trying to redirect python's stdout to another location. The reason for this is that I'm embedding python in an application. Now, originally my code was developed for Linux and that...
7
by: Cell | last post by:
when both are connected to screen and the anything written to these two constant file pointers will go onto the screen ?
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development projectplanning, coding, testing,...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.