Path: csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!aioe.org!news.stack.nl!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.001 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'mrab': 0.04; 'chunk': 0.07; 'terminated': 0.07; 'from:addr:python': 0.09; 'pos': 0.09; 'output': 0.10; 'am,': 0.12; 'def': 0.13; 'from:addr:mrabarnett.plus.com': 0.16; 'from:name:mrab': 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; '>>>': 0.18; 'suggest': 0.20; 'header:In-Reply- To:1': 0.22; 'command': 0.24; 'yield': 0.29; 'skip:b 20': 0.29; 'lines': 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; 'to:addr:python.org': 0.40; '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=YT0bOQ5lfsgqGFLX-usA:9 a=wPNLvfGTeEIA:10 a=0nF1XD0wxitMEM03M9B4ZQ==:117 X-AUTH: mrabarnett:2500 Date: Fri, 23 Dec 2011 01:18:48 +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> In-Reply-To: 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: 42 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1324603122 news.xs4all.nl 6916 [2001:888:2000:d::a6]:38449 X-Complaints-To: abuse@xs4all.nl Xref: x330-a1.tempe.blueboxinc.net comp.lang.python:17762 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