osdir.com

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Tutor] Error Python version 3.6 does not support this syntax.


On 11/29/18 12:20 PM, srinivasan wrote:
> Dear Python Experts,
> 
> With the below code snippet, I am seeing the below error, I am using
> python 3.6, could you please what could be the issue?

>         self.child = pexpect.spawn("bluetoothctl", echo = False)
...
>         self.child.send(command + "\n")
>         time.sleep(pause)
>         start_failed = self.child.expect(["bluetooth", pexpect.EOF])
...
>         return self.child.before.split("\r\n")
> ------------------------------------------------------------------------------->
> the issue seems to be here
> line 27, in get_output
>     return self.child.before.split("\r\n")
> TypeError: a bytes-like object is required, not 'str'

your types don't match. it's Python 3 so what you get back from talking
to an external process is a bytes object. you're calling the split
method on that object, but passing it a string to split on - that's what
the error is saying.  It shouldn't be any more complicated to fix that
than to give it a byte object:

return self.child.before.split(b"\r\n")

or... decode the object to a str before splitting on a string.

but since you're specifically splitting on lines, you may as well use
the splitlines method instead of the split method, since that's what it
is designed for.