How to Fix AttributeError: ‘bytes’ object has no attribute ‘seek’

‘bytes’ object has no attribute ‘seek’

AttributeError: ‘bytes’ object has no attribute ‘seek’ error occurs when you try to “call the seek() method on a bytes object, but it does not exist.”

Why the error occurs

Here are two reasons for the error.

  1. When you use the seek() method on a raw bytes object, which doesn’t support it.
  2. You mistakenly thought a variable was a file-like object when it was just a bytes object.

Reproducing the error

data = b"Hello, world!"

print(data.seek(0))

Output

AttributeError: 'bytes' object has no attribute 'seek'

How to fix it?

To fix the AttributeError: ‘bytes’ object has no attribute ‘seek’ error, “wrap the bytes object as a file-like object in an io.BytesIO instance.”

import io

data = b"Hello, world!"
buffer = io.BytesIO(data)

print(buffer.seek(0))

Output

0

If you mistakenly thought the variable was a file-like object, review your code to understand why it contains raw bytes and adjust accordingly.

Related posts

AttributeError: ‘bytes’ object has no attribute ‘read’

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.