You need to change count of reading bytes in chunk because some time len(str) != len(str in bytes) So you need to change line `total_len += len(line)` to `total_len += len(line.encode())`