Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #90848

Re: Why does the first loop go wrong with Python3

References <878uckvjoy.fsf@Equus.decebal.nl>
From Oscar Benjamin <oscar.j.benjamin@gmail.com>
Date 2015-05-19 14:16 +0100
Subject Re: Why does the first loop go wrong with Python3
Newsgroups comp.lang.python
Message-ID <mailman.133.1432041396.17265.python-list@python.org> (permalink)

Show all headers | View raw


On 19 May 2015 at 13:24, Cecil Westerhof <Cecil@decebal.nl> 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

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

Why does the first loop go wrong with Python3 Cecil Westerhof <Cecil@decebal.nl> - 2015-05-19 14:24 +0200
  Re: Why does the first loop go wrong with Python3 Oscar Benjamin <oscar.j.benjamin@gmail.com> - 2015-05-19 14:16 +0100
    Re: Why does the first loop go wrong with Python3 Thomas Rachel <nutznetz-0c1b6768-bfa9-48d5-a470-7603bd3aa915@spamschutz.glglgl.de> - 2015-05-19 16:38 +0200
    Re: Why does the first loop go wrong with Python3 Cecil Westerhof <Cecil@decebal.nl> - 2015-05-19 16:44 +0200
      Re: Why does the first loop go wrong with Python3 Ian Kelly <ian.g.kelly@gmail.com> - 2015-05-19 09:49 -0600
        Re: Why does the first loop go wrong with Python3 Cecil Westerhof <Cecil@decebal.nl> - 2015-05-19 18:39 +0200
          Re: Why does the first loop go wrong with Python3 Chris Angelico <rosuav@gmail.com> - 2015-05-20 03:11 +1000

csiph-web