Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > redirect standard output problem

Reply
Thread Tools

redirect standard output problem

 
 
iMath
Guest
Posts: n/a
 
      12-21-2012
redirect standard output problem

why the result only print A but leave out 888 ?

import sys
class RedirectStdoutTo:

def __init__(self, out_new):
self.out_new = out_new
def __enter__(self):
sys.stdout = self.out_new
def __exit__(self, *args):
sys.stdout = sys.__stdout__


print('A')
with open('out.log', mode='w', encoding='utf-8') as a_file, RedirectStdoutTo(a_file):

print('B')
print('C')

print(88
 
Reply With Quote
 
 
 
 
Chris Angelico
Guest
Posts: n/a
 
      12-21-2012
On Fri, Dec 21, 2012 at 4:23 PM, iMath <(E-Mail Removed)> wrote:
> redirect standard output problem
>
> why the result only print A but leave out 888 ?


No idea, because when I paste your code into the Python 3.3
interpreter or save it to a file and run it, it does exactly what I
would expect. A and 888 get sent to the screen, B and C go to the
file.

What environment are you working in? Python version, operating system,
any little details that just might help us help you.

ChrisA
 
Reply With Quote
 
 
 
 
Hans Mulder
Guest
Posts: n/a
 
      12-21-2012
On 21/12/12 06:23:18, iMath wrote:
> redirect standard output problem
>
> why the result only print A but leave out 888 ?
>
> import sys
> class RedirectStdoutTo:
>
> def __init__(self, out_new):
> self.out_new = out_new
> def __enter__(self):
> sys.stdout = self.out_new
> def __exit__(self, *args):
> sys.stdout = sys.__stdout__
>
>
> print('A')
> with open('out.log', mode='w', encoding='utf-8') as a_file, RedirectStdoutTo(a_file):
>
> print('B')
> print('C')
>
> print(88


On my machine it works as you'd expect.

If it doesn't work on your system, it might help to flush
sys.stdout in a few strategic places:

class RedirectStdoutTo:

def __init__(self, out_new):
self.out_new = out_new
def __enter__(self):
sys.stdout.flush()
sys.stdout = self.out_new
def __exit__(self, *args):
sys.stdout.flush()
sys.stdout = sys.__stdout__


print('A')
with open('out.log', mode='w', encoding='utf-8') as a_file,
RedirectStdoutTo(a_file):

print('B')
print('C')

print(88
sys.stdout.flush()


Hope this helps,

-- HansM
 
Reply With Quote
 
Dave Angel
Guest
Posts: n/a
 
      12-21-2012
On 12/21/2012 12:23 AM, iMath wrote:
> redirect standard output problem
>
> why the result only print A but leave out 888 ?
>
> import sys
> class RedirectStdoutTo:
>
> def __init__(self, out_new):
> self.out_new = out_new
> def __enter__(self):
> sys.stdout = self.out_new
> def __exit__(self, *args):
> sys.stdout = sys.__stdout__


Just a comment. It'd be better to save and restore the stdout value,
rather than restoring it to what it was at begin of process.

Quoting from
http://docs.python.org/3.3/library/s...sys.__stdout__

It can also be used to restore the actual files to known working file
objects in case they have been overwritten with a broken object.
However, the preferred way to do this is to explicitly save the previous
stream before replacing it, and restore the saved object.


My reasoning is that some function further up the stack may also be
using a similar (or identical) redirection technique. Anyway, instead
of using __stdout__, I'd use a saved value inside your object.

def __init__(self, out_new):
self.out_new = out_new
def __enter__(self):
self.savedstate = sys.stdout
sys.stdout = self.out_new
def __exit__(self, *args):
sys.stdout = self.savedstate

This may or may not solve your problem (I'd suspect that flush() is the
right answer), but I still think it's worth doing.



--

DaveA

 
Reply With Quote
 
iMath
Guest
Posts: n/a
 
      12-22-2012
在 2012年12月21日星期五UTC+8下午3时24分10 ,Chris Angelico写道:
> On Fri, Dec 21, 2012 at 4:23 PM, iMath <(E-Mail Removed)> wrote:
>
> > redirect standard output problem

>
> >

>
> > why the result only print A but leave out 888 ?

>
>
>
> No idea, because when I paste your code into the Python 3.3
>
> interpreter or save it to a file and run it, it does exactly what I
>
> would expect. A and 888 get sent to the screen, B and C go to the
>
> file.
>
>
>
> What environment are you working in? Python version, operating system,
>
> any little details that just might help us help you.
>
>
>
> ChrisA



on WinXP + python32

when I run it through command line ,it works ok ,but when I run it through IDLE , only print A but leave out 888
so why ?
 
Reply With Quote
 
iMath
Guest
Posts: n/a
 
      12-22-2012
在 2012年12月21日星期五UTC+8下午3时24分10 ,Chris Angelico写道:
> On Fri, Dec 21, 2012 at 4:23 PM, iMath <(E-Mail Removed)> wrote:
>
> > redirect standard output problem

>
> >

>
> > why the result only print A but leave out 888 ?

>
>
>
> No idea, because when I paste your code into the Python 3.3
>
> interpreter or save it to a file and run it, it does exactly what I
>
> would expect. A and 888 get sent to the screen, B and C go to the
>
> file.
>
>
>
> What environment are you working in? Python version, operating system,
>
> any little details that just might help us help you.
>
>
>
> ChrisA



on WinXP + python32

when I run it through command line ,it works ok ,but when I run it through IDLE , only print A but leave out 888
so why ?
 
Reply With Quote
 
Chris Angelico
Guest
Posts: n/a
 
      12-22-2012
On Sun, Dec 23, 2012 at 2:07 AM, iMath <(E-Mail Removed)> wrote:
> when I run it through command line ,it works ok ,but when I run it through IDLE , only print A but leave out 888
> so why ?


Because IDLE has to fiddle with stdin/stdout a bit to function. Try
adopting Dave's recommendation - you'll likely find that it then
works.

Python 3.3.0 (v3.3.0:bd8afb90ebf2, Sep 29 2012, 10:55:4 [MSC v.1600
32 bit (Intel)] on win32
>>> sys.stdout is sys.__stdout__

True

The same in IDLE:
>>> sys.stdout is sys.__stdout__

False

Whenever you work in IDLE, expect that some things will be slightly
different, mostly to do with standard I/O streams. So when you post
issues that you've come across, please state that you're using IDLE -
it likely makes a difference!

ChrisA
 
Reply With Quote
 
Terry Reedy
Guest
Posts: n/a
 
      12-22-2012
On 12/22/2012 10:15 AM, Chris Angelico wrote:
> On Sun, Dec 23, 2012 at 2:07 AM, iMath <(E-Mail Removed)> wrote:
>> when I run it through command line ,it works ok ,but when I run it through IDLE , only print A but leave out 888
>> so why ?

>
> Because IDLE has to fiddle with stdin/stdout a bit to function. Try
> adopting Dave's recommendation - you'll likely find that it then
> works.
>
> Python 3.3.0 (v3.3.0:bd8afb90ebf2, Sep 29 2012, 10:55:4 [MSC v.1600
> 32 bit (Intel)] on win32
>>>> sys.stdout is sys.__stdout__

> True
>
> The same in IDLE:
>>>> sys.stdout is sys.__stdout__

> False


In particular, on Windows,
>>> import sys
>>> sys.stdout

<idlelib.run._RPCOutputFile object at 0x0000000002D852E8>
>>> sys.__stdout__
>>>


In other words, sys.__stdout__ is None!

To explain: IDLE runs in two processes connected by sockets. There is
one process for user code and one for the gui. The user code process
should be invisible as the gui process handles all user interaction. On
Windows, this mean a process with no window and no input/output, which
is what the pythonw (windowless) exe is for. (I believe IDLE on *nix
uses an invisible window instead.) In any case, sys.stdout is set to a
remote procedure call object that sends output to the socket connection.
The gui process reads the socket and call the tkinter method to all text
to a tk text box.

> Whenever you work in IDLE, expect that some things will be slightly
> different, mostly to do with standard I/O streams. So when you post
> issues that you've come across, please state that you're using IDLE -
> it likely makes a difference!


Indeed! Especially for i/o issues, retest in the command window.

--
Terry Jan Reedy

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
How to redirect a "system" standard output and standard error to avariable (Linux) Venks Ruby 5 12-06-2007 12:21 AM
Redirect Standard output to keyboard port JimWilh Java 6 06-29-2005 03:21 PM
Basic Q - Response.Redirect, all redirect to first Response.Redirect statement Sal ASP .Net Web Controls 1 05-15-2004 03:46 PM
Re: How to redirect the standard output to a file? Roedy Green Java 0 08-19-2003 07:06 PM
Re: How to redirect the standard output to a file? KC Wong Java 0 08-19-2003 08:18 AM



Advertisments