Item assignment to bytes object?

Bytestrings (and strings in general) are immutable objects in Python. Once you create them, you can't change them. Instead, you have to create a new one that happens to have some of the old content. (For instance, with a basic string, newString = oldString[:offset] + newChar + oldString[offset+1:] or the like.)

Instead, you may want to convert the bytestring to a list of bytes first, or a bytearray, manipulate it, and then convert the bytearray/list back to a static string after all of the manipulations have been done. This avoids creating a new string for each replace operation.


Change the return statement of GetFileContents into

return bytearray(fileContents)

and the rest should work. You need to use bytearray rather than bytes simply because the former is mutable (read/write), the latter (which is what you're using now) is immutable (read-only).