Path: csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!weretis.net!feeder1.news.weretis.net!feeder.erje.net!newsfeed.xs4all.nl!newsfeed6.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.000 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'python.': 0.04; 'mrab': 0.04; 'chunk': 0.07; 'terminated': 0.07; '>>>>': 0.09; 'from:addr:python': 0.09; 'pos': 0.09; 'output': 0.10; 'am,': 0.12; 'def': 0.13; 'different,': 0.16; 'from:addr:mrabarnett.plus.com': 0.16; 'from:name:mrab': 0.16; 'helped.': 0.16; 'message-id:@mrabarnett.plus.com': 0.16; 'read()': 0.16; 'received:84.92': 0.16; 'received:84.92.122': 0.16; 'received:84.92.122.60': 0.16; 'reply-to:addr:python-list': 0.16; 'wrote:': 0.18; 'thanks,': 0.18; '>>>': 0.18; 'suggest': 0.20; 'appropriate': 0.22; 'wrote': 0.22; 'header:In-Reply-To:1': 0.22; 'string': 0.24; 'command': 0.24; 'pass': 0.29; 'yield': 0.29; 'skip:b 20': 0.29; 'second': 0.29; 'correct': 0.29; 'lines': 0.30; 'if,': 0.30; 'hi,': 0.32; 'break': 0.32; 'header:User- Agent:1': 0.33; 'to:addr:python-list': 0.34; 'received:84': 0.34; 'file.': 0.34; 'parse': 0.34; 'reply-to:addr:python.org': 0.34; 'but': 0.37; 'using': 0.38; 'some': 0.38; 'should': 0.39; 'to:addr:python.org': 0.40; 'your': 0.61; 'kindly': 0.65; 'header :Reply-To:1': 0.71; 'reply-to:no real name:2**0': 0.72; 'alternative.': 0.84 X-CM-Score: 0.00 X-CNFS-Analysis: v=2.0 cv=JaQ+XD2V c=1 sm=1 a=0nF1XD0wxitMEM03M9B4ZQ==:17 a=9jsOeB20M3cA:10 a=OUOv7kDek9cA:10 a=8nJEP1OIZ-IA:10 a=6diTvc7a_e88adeOM84A:9 a=wPNLvfGTeEIA:10 a=0nF1XD0wxitMEM03M9B4ZQ==:117 X-AUTH: mrabarnett:2500 Date: Fri, 23 Dec 2011 02:14:36 +0000 From: MRAB User-Agent: Mozilla/5.0 (Windows NT 5.1; rv:8.0) Gecko/20111105 Thunderbird/8.0 MIME-Version: 1.0 To: python-list@python.org Subject: Re: reading multiline output References: <79DF8234-67BF-4297-ACE6-8D091D05B3E9@gmail.com> <4EF3CF8B.4060005@mrabarnett.plus.com> <4EF3D6F8.3020203@mrabarnett.plus.com> <2D5264F3-A1EB-4C68-85E2-A39C5C67E699@gmail.com> In-Reply-To: <2D5264F3-A1EB-4C68-85E2-A39C5C67E699@gmail.com> Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.12 Precedence: list Reply-To: python-list@python.org List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 54 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1324606465 news.xs4all.nl 6966 [2001:888:2000:d::a6]:36823 X-Complaints-To: abuse@xs4all.nl Xref: x330-a1.tempe.blueboxinc.net comp.lang.python:17767 On 23/12/2011 02:05, Mac Smith wrote: > > 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 > > thanks, this helped. just need to correct line_ending="\n" should be line_ending="\r" > I wrote the default as "\n" because that's the normal line ending in Python. If, as in your case, the line ending is different, just pass the appropriate string as the second argument.