Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Piping python's output

15 views
Skip to first unread message

Dave Farrance

unread,
Sep 19, 2012, 7:02:37 AM9/19/12
to
I've just been tracing a bug in a script I'd written which included the
Ubuntu "add-apt-repository" command, and it didn't output its prompt text
until *after* its operation had completed. Something to do with it being
a Python script, and me piping its output for logging.

After trying some python test-cases, I'd guess it's down to this... here's
a python one-liner that prompts for the Enter-key then says OK:

$ python -c 'import os; raw_input("Hit Enter"); os.system("echo OK")'

So far so good, but put a pipe on the output:

$ python -c 'import os; raw_input("Hit Enter"); os.system("echo OK")' |tee

And now the prompt doesn't appear until *after*. So I guess I'd better
forget about piping the output of certain Ubuntu commands for logging. Or
just get used to typing blind. Or am I missing something?

Giovanna Stefani

unread,
Sep 19, 2012, 7:39:09 AM9/19/12
to
Dave Farrance scrive:

> And now the prompt doesn't appear until *after*. So I guess I'd better
> forget about piping the output of certain Ubuntu commands for logging.
> Or just get used to typing blind. Or am I missing something?

Add a logging command at the end in place of the pipe.

--
Giovanna
Debian 6 user

Tony Houghton

unread,
Sep 19, 2012, 9:07:38 AM9/19/12
to
In <gl4j58to67ibnctqt...@4ax.com>,
How about this?:

import sys; print "Hit Enter",; sys.stdout.flush(); raw_input(""); print "OK"

(although using print is a bit awkward these days with the transition
from python 2 to 3).

--
TH * http://www.realh.co.uk

Dave Farrance

unread,
Sep 19, 2012, 12:18:10 PM9/19/12
to
Tony Houghton <h...@realh.co.uk> wrote:

>How about this?:
>
>import sys; print "Hit Enter",; sys.stdout.flush(); raw_input(""); print "OK"
>
>(although using print is a bit awkward these days with the transition
>from python 2 to 3).

OK, thanks. I didn't want to hack the "add-apt-repository" Ubuntu tool
itself, but it turned out to be easy to spot the text-output lines
describing what the tool was about to do followed by the user-prompt, so I
inserted "sys.stdout.flush()" there, and that works.

However, hacking stuff in /usr/bin is storing up trouble, so I'd rather
find a way to call the tool as-is with my bash script and still be able to
log its output. I was wondering why python is happy to output text to
stdout unless it's to a pipe making this flush command necessary. How
does it "know" that the pipe is there?

Dave Farrance

unread,
Sep 19, 2012, 1:21:33 PM9/19/12
to
Dave Farrance <DaveFa...@OMiTTHiSyahooANDTHiS.co.uk> wrote:

> ... I was wondering why python is happy to output text to
>stdout unless it's to a pipe making this flush command necessary. How
>does it "know" that the pipe is there?

Never mind. More Googling gave me the answer. It's because bash buffers
stdout in various ways. Prefixing a command with "stdbuf -oL" switches
the buffering to line-mode, and that's the solution in this case.

Graham Murray

unread,
Sep 19, 2012, 4:27:44 PM9/19/12
to
Dave Farrance <DaveFa...@OMiTTHiSyahooANDTHiS.co.uk> writes:

> However, hacking stuff in /usr/bin is storing up trouble, so I'd rather
> find a way to call the tool as-is with my bash script and still be able to
> log its output. I was wondering why python is happy to output text to
> stdout unless it's to a pipe making this flush command necessary. How
> does it "know" that the pipe is there?

It does not actually know it is a pipe. What is does know is that it is
not a terminal - isatty(3) tells it that the file descriptor is not a
terminal.
0 new messages