The
b
indicates that what you have is bytes
, which is a binary sequence of bytes rather than a string of Unicode characters. Subprocesses output bytes, not characters, so that's what communicate()
is returning.
The
bytes
type is not directly print()
able, so you're being shown the repr
of the bytes
you have. If you know the encoding of the bytes you received from the subprocess, you can use decode()
to convert them into a printable str
:>>> print(b'hi\n'.decode('ascii'))
hi
Of course, this specific example only works if you actually are receiving ASCII from the subprocess. If it's not ASCII, you'll get an exception:
>>> print(b'\xff'.decode('ascii'))
Traceback (most recent call last):
File "" , line 1, in <module>
UnicodeDecodeError: 'ascii' codec can't decode byte 0xff in position 0…
The newline is part of what
echo hi
has output. echo
's job is to output the parameters you pass it, followed by a newline. If you're not interested in whitespace surrounding the process output, you can use strip()
like so:>>> b'hi\n'.strip()
b'hi'
Comments
Post a Comment
https://gengwg.blogspot.com/