import os
file = open('File.txt','r')
for line in file:
print line
I was strugling with using readline() and going through a while loop with while file.readline() != ' ' under the assumption that and empty string is returned at the end of file. (Where did I find this?) Well online docs say
f.readline() reads a single line from the file; a newline character (\n) is left at the end of the string, and is only omitted on the last line of the file if the file doesn't end in a newline. This makes the return value unambiguous; if f.readline() returns an empty string, the end of the file has been reached, while a blank line is represented by '\n', a string containing only a single newline.
How do I match against the empty string at the end-of-file or EOF character? while != ' ': does not stop and keeps hanging a the last position of the file got by tell() method.
1 comment:
My mistake, I had to compare it against '' - empty string NOT ' ' - a single space.
Post a Comment