Path: csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!news.chainon-marquant.org!news-transit.tcx.org.uk!rt.uk.eu.org!newsfeed.xs4all.nl!newsfeed5.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.003 X-Spam-Evidence: '*H*': 0.99; '*S*': 0.00; 'mrab': 0.04; 'chunk': 0.07; 'terminated': 0.07; '>>>>': 0.09; 'pos': 0.09; 'output': 0.10; 'am,': 0.12; 'def': 0.13; 'received:209.85.210.174': 0.13; 'received:mail-iy0-f174.google.com': 0.13; 'helped.': 0.16; 'read()': 0.16; 'wrote:': 0.18; 'thanks,': 0.18; '>>>': 0.18; 'suggest': 0.20; 'header:In-Reply-To:1': 0.22; 'command': 0.24; 'url:mailman': 0.28; 'yield': 0.29; 'skip:b 20': 0.29; 'correct': 0.29; 'lines': 0.30; 'hi,': 0.32; 'break': 0.32; 'url:listinfo': 0.32; 'message-id:@gmail.com': 0.33; 'to:addr:python-list': 0.34; 'file.': 0.34; 'parse': 0.34; 'url:python': 0.36; 'charset:us- ascii': 0.37; 'but': 0.37; 'received:google.com': 0.37; 'using': 0.38; 'some': 0.38; 'received:209.85': 0.38; 'url:org': 0.39; 'should': 0.39; 'received:209': 0.40; 'to:addr:python.org': 0.40; 'header:Message-Id:1': 0.62; 'kindly': 0.65; 'received:10.10': 0.68; 'alternative.': 0.84 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=content-type:mime-version:subject:from:in-reply-to:date :content-transfer-encoding:message-id:references:to:x-mailer; bh=Z7tGH+K6S0MhMDNoTvqB55LbrMzhZhzdBOOCPpwxZgo=; b=dVZFCVfmgzZexmtMRwfvRgcSCoRXs+2YeTMS9gKJnmOJ60OqpBaZk1eu81fp7xya3s v9l4zGRsYsvJG34yf6zjKBOjcaTxo+DU3P82J02QGpJQTt6L5treHpSFFMZN0oTvVI3Q ZjZYWomoAQ5U/wZAppmcC8dQQKRw7gyr/U2xU= Content-Type: text/plain; charset=us-ascii Mime-Version: 1.0 (Apple Message framework v1251.1) Subject: Re: reading multiline output From: Mac Smith In-Reply-To: <4EF3D6F8.3020203@mrabarnett.plus.com> Date: Fri, 23 Dec 2011 07:35:23 +0530 Content-Transfer-Encoding: quoted-printable References: <79DF8234-67BF-4297-ACE6-8D091D05B3E9@gmail.com> <4EF3CF8B.4060005@mrabarnett.plus.com> <4EF3D6F8.3020203@mrabarnett.plus.com> To: python-list@python.org X-Mailer: Apple Mail (2.1251.1) X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.12 Precedence: list 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: 51 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1324605938 news.xs4all.nl 6965 [2001:888:2000:d::a6]:39060 X-Complaints-To: abuse@xs4all.nl Xref: x330-a1.tempe.blueboxinc.net comp.lang.python:17765 On 23-Dec-2011, at 6:48 AM, MRAB wrote: > On 23/12/2011 01:07, Mac Smith wrote: >>=20 >> On 23-Dec-2011, at 6:17 AM, MRAB wrote: >>=20 >>> On 23/12/2011 00:33, Mac Smith wrote: >>>> Hi, >>>>=20 >>>>=20 >>>> 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. >>>>=20 >>> The lines are terminated with \r, so read with read() and then >>> split on "\r". >>=20 >> 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. >>=20 > Try telling it how much to read with read(size): >=20 > def read_lines(output, line_ending=3D"\n"): > buffer =3D "" >=20 > while True: > chunk =3D output.read(1024) > if not chunk: > break >=20 > buffer +=3D chunk >=20 > while True: > pos =3D buffer.find(line_ending) > if pos < 0: > break >=20 > pos +=3D len(line_ending) > yield buffer[ : pos] > buffer =3D buffer[pos : ] >=20 > if buffer: > yield buffer > --=20 > http://mail.python.org/mailman/listinfo/python-list thanks, this helped. just need to correct line_ending=3D"\n" should be = line_ending=3D"\r"