Skip to content Skip to sidebar Skip to footer

Why Does Truncating A Bytesio Mess It Up?

Running this on Python 3.5.1 on OSX: import io b = io.BytesIO() b.write(b'222') print(b.getvalue()) b.truncate(0) b.write(b'222') print(b.getvalue()) Produces: b'222' b'\x00\x0

Solution 1:

truncate does not move the file pointer. So the next byte is written to the next position. You have also to seek to the beginning:

b.seek(0)
b.truncate()

Post a Comment for "Why Does Truncating A Bytesio Mess It Up?"