0

I am reading gmail messages (using the code from accepted answer)

(retcode, messages) = conn.search(None, '(UNSEEN)')
if retcode == 'OK':
    for num in messages[0].split(' '): # messages[0] is b'6' in my case.

It's throwing, "TypeError: a bytes-like object is required, not 'str'". But I can see clearly its a byte object b'6'

Tried same on python shell as well and getting same error there as well. Not sure whats wrong here.

>>> b'6'.split(' ')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: a bytes-like object is required, not 'str'
Laxmikant
  • 2,046
  • 3
  • 30
  • 44

1 Answers1

1

When it gives you the TypeError, it is not referring to the b'6'... it is rather referring to the ' ' in .split() -- it is trying to split a bytes object with a string. To fix this, simply change the line to:

(retcode, messages) = conn.search(None, '(UNSEEN)')
if retcode == 'OK':
    for num in messages[0].split(b' '): # messages[0] is b'6' in my case.

Or, in the case of the python shell

>>> b'6'.split(b' ')
[b'6']
lyxαl
  • 1,108
  • 1
  • 16
  • 26