In answer to your question, your empty line comes from:
print >> file, line
Using print , which automatically prints a newline, use sys.stdout.write or use a sys.stdout.write comma to suppress the newline character, for example:
print >> file, line,
In any case, the best way to approach this overall is to use itertools.islice for:
from itertools import islice with open('input') as fin, open('output', 'w') as fout: fout.writelines(islice(fin, None, None, 2))
And if necessary, filter the empty lines first, and then take every second of this ...
non_blanks = (line for line in fin if line.strip()) fout.writelines(islice(non_blanks, None, None, 2))
It is much more convenient and flexible than thinning with a module, etc.
source share