write multiple lines in a file in python

write multiple lines in a file in python

Youre confusing the braces. Do it like this:

target.write(%s n %s n %s n % (line1, line2, line3))

Or even better, use writelines:

target.writelines([line1, line2, line3])

another way which, at least to me, seems more intuitive:

target.write(line 1
line 2
line 3)

write multiple lines in a file in python

with open(target.txt,w) as out:
    line1 = raw_input(line 1: )
    line2 = raw_input(line 2: )
    line3 = raw_input(line 3: )
    print(Im going to write these to the file.)
    out.write({}n{}n{}n.format(line1,line2,line3))

Leave a Reply

Your email address will not be published. Required fields are marked *