No EOF(), the Python way

by Forrest Sheng Bao http://fsbao.net

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: http://groups.google.com/group/comp.lang.python/browse_thread/thread/ed25388487b3ac7b

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". http://docs.python.org/lib/bltin-file-objects.html

No comments: