Your problem is that you misused for
cycle. When you write for
cycle as for index in 10
it will be executed 10 times. In your case, you have for bytes in file.get_len():
. If your file has 1000 bytes, then it will be executed as for bytes in 1000
, i.e. this for cycle will be executed 1000 times. And this is obviously not what you want. Instead of your cycle
for bytes in file.get_len():
bytearray += file.get_buffer(bytes)
you just have to read file as
bytearray = file.get_buffer(file.get_len())
After this change, everything will work as expected.
Right now your code prints a lot of garbage because file.get_buffer
attempts to read content that is outside of your file (if you file has 4 bytes, then at first cycle step you will read 1 byte (3 remain), at second step 2 bytes (1 byte remain), and at 3'rd step you will try to read 3 bytes, but only 1 byte remain, so you would grab 2 bytes of garbage. Last step will have 4 bytes of garbage, because you already outside of your file, experiencing undefined behavior.