Python Interactive Shell - outputting to stdout?

Steve Holden steve at holdenweb.com
Thu Dec 23 15:18:58 EST 2004


Avi Berkovich wrote:

> Hello,
> 
> I was unable to use popen2.popen4 to grab python.exe's (2.3) output, for 
> starts, it doesn't show the version information at the beginning and 
> won't return anything when writing to the stdin pipe, it seems that if I 
> give it some error nous expression, the pipe would return the exception 
> data, though nothing else comes through.
> 
> A friend of mine also tried this using win32api on delphi, and got the 
> same result.
> 
> I also tried "python.exe > data.txt" and was amazed to find out that it 
> won't redirect the output to the file.
> 
> So, may someone please shed some light over this?
> 
> Avi.

Well, your first misunderstanding appears to be the omission of the 
standard error file. Interactive messages and prompts are written to 
stderr, output to stdout:

C:\Steve>python.exe > python.txt
Python 2.4 (#60, Nov 30 2004, 11:49:19) [MSC v.1310 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
  >>> 123
  >>> 345
  >>> ^Z


C:\Steve>type python.txt
123
345

When you try to write a program that communicates with a client that's 
intended for interactive use (such as the Python interpreter when called 
with no arguments), you have the problem of divining exactly when the 
client is waiting for input.

Rather than blame the Python interpreter, first write a program that 
interacts with (say) the Windows command-line interpreter.

If your experience is substantially different then it *might* be a 
problem with the interpreter. If your experience is more or less the 
same then the problem is probably to do with the nature of the 
interactions, which are bad enough when only considering stdin and 
stdout. When you add the stderr stream into the picture it's sometimes 
impossible to know when the interactive client is waiting for input.

One final comment: you haven't so far said *why* you want to interact 
with Python in this way. Given the availability of exec and eval(), just 
what is it that you are trying to do that they won't let you?

regards
  Steve
-- 
Steve Holden               http://www.holdenweb.com/
Python Web Programming  http://pydish.holdenweb.com/
Holden Web LLC      +1 703 861 4237  +1 800 494 3119



More information about the Python-list mailing list