Path: csiph.com!usenet.pasdenom.info!weretis.net!feeder4.news.weretis.net!feeds.phibee-telecom.net!newsfeed.xs4all.nl!newsfeed4a.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!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; 'output': 0.05; 'string.': 0.05; 'true,': 0.05; 'memory.': 0.07; 'python3': 0.07; 'bytes.': 0.09; 'executed': 0.09; 'iterate': 0.09; 'subject:Python3': 0.09; 'subject:Why': 0.09; 'cc:addr:python-list': 0.11; 'python': 0.11; '(both': 0.16; '__future__': 0.16; "b''": 0.16; 'big,': 0.16; 'cc:name:python list': 0.16; 'division,': 0.16; 'python3.': 0.16; 'sentinel': 0.16; 'stdout': 0.16; 'wrote:': 0.18; 'feb': 0.22; '>>>': 0.22; 'import': 0.22; 'shell': 0.22; 'cc:addr:python.org': 0.22; 'bytes': 0.24; 'cc:2**0': 0.24; 'second': 0.26; 'code:': 0.26; 'header:In-Reply-To:1': 0.27; 'skip:p 30': 0.29; 'returned': 0.30; 'message-id:@mail.gmail.com': 0.30; 'work.': 0.31; 'continually': 0.31; 'prints': 0.31; 'text': 0.33; 'becomes': 0.33; 'checking': 0.33; 'subject:the': 0.34; 'problem': 0.35; 'subject:with': 0.35; 'but': 0.35; 'received:google.com': 0.35; 'version': 0.36; 'false': 0.36; 'skip:p 20': 0.39; 'how': 0.40; "you're": 0.61; 'first': 0.61; 'more': 0.64; 'between': 0.67; 'carefully': 0.74; '2014,': 0.84; '2015': 0.84; 'oscar': 0.84 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:from:date:message-id:subject:to :cc:content-type; bh=bK2Wgr2yPbyXr3ieRpSrjYJvkYXI1fWspR4FutH1O4o=; b=pH+qGVb2sg5CpIp6jPLRRBs2wYHHYbZX3eC0hx4QIP3f6BkKE1ZVRMQ+WgYLV5vDEr GODKRd/R8zSk317yq039IUOlMS6KpJSucbiKvNy/nqYwFPFDTjTkNWbkjmMxmsaL57bA Ej9PZTIZF6NN1jKBZD19ysA63TVvHJo8J1+14uOmI0kYwgXKLgJqfOg+gwjnGtgmaPaS 80tdkqLip9SgnZhuIRJlFlh5AghQySxDIxTmiEg0cNWe07ww9wQZwrhVcj/gKePsrWSj sX+KlpI6gpImYf1Lbyqe85Oe6BOFtAoVf49w2LFHTutxlm6X0hRxWMgJ9vCZ3mlhrUCV CR9w== X-Received: by 10.180.91.76 with SMTP id cc12mr32411501wib.67.1432041393566; Tue, 19 May 2015 06:16:33 -0700 (PDT) MIME-Version: 1.0 In-Reply-To: <878uckvjoy.fsf@Equus.decebal.nl> References: <878uckvjoy.fsf@Equus.decebal.nl> From: Oscar Benjamin Date: Tue, 19 May 2015 14:16:13 +0100 Subject: Re: Why does the first loop go wrong with Python3 To: Cecil Westerhof Cc: Python List Content-Type: text/plain; charset=UTF-8 X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.20+ 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: 49 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1432041396 news.xs4all.nl 2906 [2001:888:2000:d::a6]:51082 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:90848 On 19 May 2015 at 13:24, Cecil Westerhof wrote: > I have the following code: > from __future__ import division, print_function > > import subprocess > > p = subprocess.Popen('ls -l', shell = True, stdout = subprocess.PIPE) > for line in iter(p.stdout.readline, ''): > print(line.rstrip().decode('utf-8')) > > p = subprocess.Popen('ls -l', shell = True, stdout = subprocess.PIPE) > for line in p.stdout.readlines(): > print(line.rstrip().decode('utf-8')) > > This works in Python2. (Both give the same output.) > > But when I execute this in Python3, then the first loop is stuck in a > loop where it continually prints a empty string. The second loop is > executed correctly in Python3. > > In the current case it is not a problem for me, but when the output > becomes big, the second solution will need more memory. How can I get > the first version working in Python3? The problem is that Python 3 carefully distinguishes between the bytes that come when reading from the stdout of a process and text which must be decoded from the bytes. You're using iter(f, sentinel) and checking for a sentinel value of ''. However in Python 3 the sentinel returned will be b''. Consider: $ python3 Python 3.2.3 (default, Feb 27 2014, 21:31:18) [GCC 4.6.3] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>> '' == b'' False If you change it from '' to b'' it will work. However the normal way to do this is to iterate over stdout directly: p = subprocess.Popen('ls -l', shell = True, stdout = subprocess.PIPE) for line in p.stdout: print(line.rstrip().decode('utf-8')) -- Oscar