From: Ben Kaplan on

On Jul 14, 2010, at 8:38 AM, Alan wrote:

> Hi there,
>
> Module commands is gone in python3, so I am trying subprocess. So please I would appreciate if someone can tell me how to do this better:
>
> before I had:
>
> cmd = 'uname -a'
> out = commands.getoutput(cmd)
>
> 'Darwin amadeus.local 10.4.0 Darwin Kernel Version 10.4.0: Fri Apr 23 18:28:53 PDT 2010; root:xnu-1504.7.4~1/RELEASE_I386 i386 i386 MacBookPro5,2 Darwin'
>
> now:
>
> out = sub.Popen(cmd, shell=True, stderr = sub.STDOUT, stdout = sub.PIPE).communicate()[0][:-1]
>
> b'Darwin amadeus.local 10.4.0 Darwin Kernel Version 10.4.0: Fri Apr 23 18:28:53 PDT 2010; root:xnu-1504.7.4~1/RELEASE_I386 i386 i386 MacBookPro5,2 Darwin'
>
> Yes, it's ugly. the [:-1] above is to get read of the last '\n' which with getoutputs I didn't have. But what's giving headache is this "b'..." in the beginning.
>
> Can someone explain, point me to where I can now about it and how to make this better? I wanted a plain string in out.
>
> Many thanks in advance,
>
> Alan

There are 2 string types in Python: byte strings and unicode strings. In Python 2.x, they were called str and unicode, the default was str, and unicode was signaled by prefixing the string with a u. In python 3.x, they are called bytes and str. str (which is what used to be unicode) is the default, and a byte string (what used to be str) is signaled by putting a b in front of the string.

Unicode is an abstract concept. Python can move it around internally, but the only thing you can send to other computers and programs is a sequence of bytes. If you want to convert the byte string to a unicode string, you have to decode it afterwards.

out = out.decode("utf-8")


From: Gary Herron on
On 07/14/2010 08:38 AM, Alan wrote:
> Hi there,
>
> Module commands is gone in python3, so I am trying subprocess. So
> please I would appreciate if someone can tell me how to do this better:
>
> before I had:
>
> cmd = 'uname -a'
> out = commands.getoutput(cmd)
>
> 'Darwin amadeus.local 10.4.0 Darwin Kernel Version 10.4.0: Fri Apr 23
> 18:28:53 PDT 2010; root:xnu-1504.7.4~1/RELEASE_I386 i386 i386
> MacBookPro5,2 Darwin'
>
> now:
>
> out = sub.Popen(cmd, shell=True, stderr = sub.STDOUT, stdout =
> sub.PIPE).communicate()[0][:-1]
>
> b'Darwin amadeus.local 10.4.0 Darwin Kernel Version 10.4.0: Fri Apr 23
> 18:28:53 PDT 2010; root:xnu-1504.7.4~1/RELEASE_I386 i386 i386
> MacBookPro5,2 Darwin'
>
> Yes, it's ugly. the [:-1] above is to get read of the last '\n' which
> with getoutputs I didn't have. But what's giving headache is this
> "b'..." in the beginning.
>
> Can someone explain, point me to where I can now about it and how to
> make this better? I wanted a plain string in out.
>
> Many thanks in advance,
>
> Alan
> --
> Alan Wilter S. da Silva, D.Sc. - CCPN Research Associate
> Department of Biochemistry, University of Cambridge.
> 80 Tennis Court Road, Cambridge CB2 1GA, UK.
> >>http://www.bio.cam.ac.uk/~awd28 <http://www.bio.cam.ac.uk/%7Eawd28><<


The 'b' is not part of the returned value. If you look at the *type* of
the returned value, you will find that it is not a string, but rather a
byte array. Printing of byte arrays displays the b'...' to indicate
the type of thing begin printed.

If you want a string, then you must convert the byte array to a
string. For instance:

str_out = out.decode('ascii')

(And remember: in Python3, strings are always unicode.)

Also, using out.strip() or str_out.strip() may be a better way to remove
the white space.

Gary Herron




From: Dave Angel on
Alan wrote:
> Hi there,
>
> Module commands is gone in python3, so I am trying subprocess. So please I
> would appreciate if someone can tell me how to do this better:
>
> before I had:
>
> cmd = 'uname -a'
> out = commands.getoutput(cmd)
>
> 'Darwin amadeus.local 10.4.0 Darwin Kernel Version 10.4.0: Fri Apr 23
> 18:28:53 PDT 2010; root:xnu-1504.7.4~1/RELEASE_I386 i386 i386 MacBookPro5,2
> Darwin'
>
> now:
>
> out = sub.Popen(cmd, shell=True, stderr = sub.STDOUT, stdout =
> sub.PIPE).communicate()[0][:-1]
>
> b'Darwin amadeus.local 10.4.0 Darwin Kernel Version 10.4.0: Fri Apr 23
> 18:28:53 PDT 2010; root:xnu-1504.7.4~1/RELEASE_I386 i386 i386 MacBookPro5,2
> Darwin'
>
> Yes, it's ugly. the [:-1] above is to get read of the last '\n' which with
> getoutputs I didn't have. But what's giving headache is this "b'..." in the
> beginning.
>
> Can someone explain, point me to where I can now about it and how to make
> this better? I wanted a plain string in out.
>
> Many thanks in advance,
>
> Alan
>

In Python3, 'plain string' would be unicode. But communicate returns
bytes. So convert them to a string, for example, by using str(). By
default that'll assume the bytes are ASCII encoded, so if you have any
characters above 7f, you'd need something more.

DaveA