reading multiline output

Cameron Simpson cs at zip.com.au
Thu Dec 22 20:21:40 EST 2011


On 23Dec2011 06:37, Mac Smith <macsmith.us at gmail.com> wrote:
| On 23-Dec-2011, at 6:17 AM, MRAB wrote:
| > On 23/12/2011 00:33, Mac Smith wrote:
| >> I have started HandBrakeCLI using subprocess.popen but the output is
| >> multiline and not terminated with \n so i am not able to read it
| >> using readline() while the HandBrakeCLI is running. kindly suggest
| >> some alternative. i have attached the output in a file.
| >> 
| > The lines are terminated with \r, so read with read() and then split on
| > "\r".
| 
| read() will read the complete output and than i will be able to parse
| it, i want to read the output of the command in realtime.

You can read a limited number of bytes - for your purposes this needs to
be 1 byte at a time. Very inefficient, but you can do what you need.
Write a little function that gathers single bytes until "\r" and then
returns them joined into a string.

Traditionally the way to work with "interactive" facilities like this is
often the "expect" command, and I gather there's a third party Python
module named "pexpect" with that functionality. This:
  http://www.noah.org/wiki/pexpect

And then there's all sorts of read-with-timeout games you can play I
guess...

Note that all this relies on HandBrakeCLI emitting its progress messages
in a timely (== unbuffered) manner when its output is a pipe...

Cheers,
-- 
Cameron Simpson <cs at zip.com.au> DoD#743
http://www.cskk.ezoshosting.com/cs/

It's better to be DoD and cool, than to get a life and be uncool!
        - _Harley Davidson & The Marlboro Man_
          (slightly paraphrased by Roar Larsen, DoD#463
                                   <Roar.Larsen at termo.unit.no>)



More information about the Python-list mailing list