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

Popular posts from this blog

node.js - Mongoose: Cast to ObjectId failed for value on newly created object after setting the value -

[C++][SFML 2.2] Strange Performance Issues - Moving Mouse Lowers CPU Usage -

ios - Possible to get UIButton sizeThatFits to work? -