You do not need to know the length of your file. There are iterators in python files that output strings. No need to use c-style for-loops.
So something like this should work for you (python3 or from __future__ import print_function ):
with open('file.in') as infile, open('file.out', 'w') as outfile: for line in infile: print(line.strip() + ' ', file=outfile)
source share