JackSparow
JackSparow

Reputation: 179

TypeError: a bytes-like object is required, not 'str' : Getting this error

Getting error : Conflict between bytes and str type

old_server_uuid = p3.communicate()[0].split("|")[1].strip()

Getting the below error:

Traceback (most recent call last):
  File "create_env_file.py", line 68, in <module>
    data = create_env_source_list(node_name, ip_address)
  File "create_env_file.py", line 14, in create_env_source_list
    raise(ex)
  File "create_env_file.py", line 12, in create_env_source_list
    old_server_uuid = p3.communicate()[0].split("|")[1].strip()
TypeError: a bytes-like object is required, not 'str'

Upvotes: 0

Views: 87

Answers (1)

Mark Tolonen
Mark Tolonen

Reputation: 178021

Python 3 doesn't allow mixing text (Unicode) strings and byte strings.

the result of p3.communicate()[0] is a byte string, so the .split argument must be one as well.

Examples:

Splitting a byte string with a Unicode string:

>>> b'abc|123'.split('|')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: a bytes-like object is required, not 'str'

Splitting a byte string with a byte string:

>>> b'abc|123'.split(b'|')
[b'abc', b'123']

Upvotes: 1

Related Questions