0
126 Jun 20, 2013 at 04:23

I’m trying to figure out what the error is to this line:

binary_format="<"+str(name_length)+"s"
data=struct.pack(binary_format, self.name)


struct.error: the argument for ‘s’ must be a bytes object

struct is a module that was imported.

The line highlighted by the error shows the second line. Are they talking about the “s” that was added to the other symbols, and why would it need to be a byte object?
This apparently has something to do with going from python 2 to python 3. I tried converting binary format with bytes(binary_format,’utf-8’) but it didn’t work.

I tried this:

name_length=len(self.name)+1
binary_format= bytes("<",'utf-8')+ bytes(str(name_length),'utf-8')+ bytes("s",'utf-8')
data=struct.pack(binary_format, self.name)


but I still get the same error.