-2

I have run into a problem while trying some code I found here on Stack Overflow:

os_name = os_info.Name.encode('utf-8').split('|')[0]

Error:

a bytes-like object is required, not 'str'

I found out that there could be problem that I use Python 3, and not Python 2.

sjakobi
  • 3,546
  • 1
  • 25
  • 43

1 Answers1

1

You encoded your string to a bytes object, but then tried to split it with a string object. bytes.split() only takes a bytes value:

os_name = os_info.Name.encode('utf-8').split(b'|')[0]

The answer you found was actually meant to work on Python 3; I've edited to correct the mistake.

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
  • Thank you very much, that did the trick someone made me confused like this os_name = os_info.Name.encode(b'utf-8').split('|')[0] – Gintaras P. Nov 01 '17 at 17:42