Python 3.x read text file without ignoring blank line at the end -
i've tried read text file that:
line 1 line 3
using following code:
with open('textfile.txt', 'r') f_in: line in f_in: print("~ " + line) f_in.closed but loop runs 3 times instead of 4, ignoring blank line.
the output is:
~ line 1 ~ ~ line 3 how can fix it?
when using file iterator, end of line marked \n. means though editor may show "blank line" after 3, if there no data there, there no line there.
for example, try this:
with open('textfile.txt', 'wb') f_out: f_out.write(b'1\n\n3\n4') open('textfile.txt', 'r') f_in: line in f_in: print("~ " + line) by putting character 4 on last line, no terminating linefeed, python recognizes line. without 4, have 3 lines.
Comments
Post a Comment