You get the ValueError because your code probably has for line in original:
in addition to original.readline()
. An easy solution which fixes the problem without making your program slower or consume more memory is changing
for line in original:
...
to
while True:
line = original.readline()
if not line: break
...