Seemingly random decoding errors I'm working on an import export addon, and while importing a skeleton I'm running into a wide variety of errors when importing the .skl skeleton file. They all point to the same place in my script though.

class sklHeader():
    """SKL header format:
    fileType       char[8]    8    version string
    numObjects     int        4    number of objects (skeletons)
    skeltonHash    int        4    unique id numbers
    numElements    int        4    number of bones

    total size                20 bytes
    """

    def __init__(self):
        self.__format__ = '&lt;8s3i'
        self.__size__ = struct.calsize(self.__format__)
        self.fileType = None
        self.numObjects = None
        self.skeltonHash = None
        self.numElements = None

    def fromFile(self, sklFile):
        """Reads the skl header object from the raw binary file"""
        sklFile.seek(0)
        fields = struct.unpack(self.__format__, sklFile.read(self.__size__))
        (fileType, self.numObjects, self.skeletonHash, self.numElements) = fields
        self.fileType = bytes.decode(fileType)
        #self.fileType = fileType

    def toFile(self, sklFile):
        """"Writes the header object to a raw binary file"""
        data = struct.pack(self.__format__, self.fileType, self.numObjects, self.skeletonHash, self.numElements)
        sklFile.write(data)


specifically all the errors are pointing to the same line 

self.fileType = bytes.decode(fileType)


but I am getting a wide variety of errors most of them are invalid continuation bytes or invalid start bytes.

UnicodeDecodeError: utf-8 codec can't decode byte 0xc3 in position 4
UnicodeDecodeError: utf-8 codec can't decode byte 0xd5 in position 2

UnicodeDecodeError: utf-8 codec can't decode byte 0x98 in position 0
UnicodeDecodeError: utf-8 codec can't decode byte 0xc8 in position 0
UnicodeDecodeError: utf-8 codec can't decode byte 0xb8 in position 0
UnicodeDecodeError: utf-8 codec can't decode byte 0xa4 in position 0
UnicodeDecodeError: utf-8 codec can't decode byte 0xa8 in position 0


All the ones in position 0 are invalid start bytes
the others are invalid continuation bytes.

Sometimes it works fine, other times I get errors such as these. 

Any ideas?


  The .py file this is in, is what actually opens the .skl file its another 200+ lines defining how to use the raw data
  
  Skl files have to be unicode or it wouldn't sometimes work correctly.
Skl files have to be unicode or it wouldn't sometimes work correctly.


That's a really vague statement. I'm pretty convinced they are binary files, which may or may not contain some unicode-encoded strings. But you definitely need to open them as binary files:

file = open(filepath, "rb")


You may then decode binary strings:

binary_object.decode("utf-8")


Reading the file in binary mode will prevent decoding errors caused by non-strings (which can be a simple '\x89' of a binary integer.