reading multiline output
Mac Smith
macsmith.us at gmail.com
Thu Dec 22 21:05:23 EST 2011
On 23-Dec-2011, at 6:48 AM, MRAB wrote:
> On 23/12/2011 01:07, Mac Smith wrote:
>>
>> On 23-Dec-2011, at 6:17 AM, MRAB wrote:
>>
>>> On 23/12/2011 00:33, Mac Smith wrote:
>>>> Hi,
>>>>
>>>>
>>>> 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.
>>
> Try telling it how much to read with read(size):
>
> def read_lines(output, line_ending="\n"):
> buffer = ""
>
> while True:
> chunk = output.read(1024)
> if not chunk:
> break
>
> buffer += chunk
>
> while True:
> pos = buffer.find(line_ending)
> if pos < 0:
> break
>
> pos += len(line_ending)
> yield buffer[ : pos]
> buffer = buffer[pos : ]
>
> if buffer:
> yield buffer
> --
> http://mail.python.org/mailman/listinfo/python-list
thanks, this helped. just need to correct line_ending="\n" should be line_ending="\r"
More information about the Python-list
mailing list