Python is adding extra newline to the output

print appends a newline, and the input lines already end with a newline.

A standard solution is to output the input lines verbatim:

import sys

with open("a.txt") as f:
    for line in f:
        sys.stdout.write(line)

PS: For Python 3 (or Python 2 with the print function), abarnert’s print(…, end='') solution is the simplest one.

Leave a Comment