No EOF(), the Python way

by Forrest Sheng Bao

Today, I was writing a program and gonna find a function to detect whether EOF, the end of file marker, was reached. But I failed. Not such function in file I/O of Python. Then I did some googling and realized that we don't need an EOF detection function. Take a look at this:

If you try to read some stuff, but you get nothing, then you have reached the EOF. Stupid and simple, ah?

My program now looks like:

while True:
testline = f.readline()
if len(testline) ==0:
break # EOF
#do some other stuff
According to Python documents, I don't think I need to do this when using readline() function, because "Read until EOF using readline() and return a list containing the lines thus read".

No comments: