Line-by-line processing when stdin is not a tty
RG
rNOSPAMon at flownet.com
Wed Aug 11 13:02:24 EDT 2010
In article <i3ud8e$p9e$02$1 at news.t-online.com>,
Peter Otten <__peter__ at web.de> wrote:
> Grant Edwards wrote:
>
> > On 2010-08-11, Tim Harig <usernet at ilthio.net> wrote:
> >> On 2010-08-11, RG <rNOSPAMon at flownet.com> wrote:
> >>> When stdin is not a tty, Python seems to buffer all the input through
> >>> EOF before processing any of it:
> >>>
> >>> [ron at mickey:~]$ cat | python
> >>> print 123
> >>> print 456 <hit ctrl-D here>
> >>> 123
> >>> 456
> >>>
> >>> Is there a way to get Python to process input line-by-line the way it
> >>> does when stdin is a TTY even when stdin is not a TTY?
> >>
> >> It would be much better to know the overall purpose of what you are
> >> trying
> >> to achieve. There are may be better ways (ie, sockets) depending what
> >> you
> >> are trying to do. Knowing your target platform would also be helpful.
> >>
> >> For the python interpeter itself, you can can get interactive behavior by
> >> invoking it with the -i option.
> >
> > If you're talking about unbuffered stdin/stdout, the option is -u.
> >
> > I don't really see how the -i option is relevent -- it causes the
> > interpreter to go into interactive mode after running the script.
>
> I'd say the following looks like what the OP was asking for:
>
> $ cat | python -i -c'import sys; sys.ps1=""'
> print sys.stdin.isatty()
> False
> print 1
> 1
> print 2
> 2
That is indeed the behavior I'm looking for.
> (Whether it's useful is yet another question)
It's useful to me :-) I'm trying to access a python library from a
program written in another language for which an equivalent library is
not available. The easiest way to do that is to spawn a Python
interpreter and interact with it through stdin/stdout.
Thanks!
rg
More information about the Python-list
mailing list