0

I'm trying to load a list back into the program from a text file using pickle:

    f = open("usernames.txt", "r")
    usernames = pickle.load(f)
    f.seek(0)
    f.truncate(0)
    f.close()

However when I run the code this error message appears:

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

How can I resolve this error?

1 Answer 1

2

You need to open the file in binary mode so that reading from it produces byte strings, not Unicode strings.

with open("usernames.txt", "rb") as f:
    usernames = pickle.load(f)

Not the answer you're looking for? Browse other questions tagged or ask your own question.