The list returned by .readlines()
, like any other Python list, has no "end" marker -- assumin e.g. you start with L = myfile.readlines()
, you can see how many items L
has with len(L)
, get the last one with L[-1]
, loop over all items one after the other with for item in L:
, and so forth ... again -- just like any other list.
If for some peculiar reason you want, after the last line, some special "sentinel" value, you could for example do L.append('')
to add just such a sentinel, or marker -- the empty string is never going to be the value of any other line (since each item is a complete line including the trailing '\n'
, so it will never be an empty string!).
You could use other arbitrary markers, such as None
, but often it's simpler, when feasible [[and it's obviously feasible in this case, as I've just shown]], if the sentinel is of exactly the same type as any other item. That depends on what processing you're doing that needs a past-the-end "sentinel" (I'm not going to guess because you give us no indication, and only relatively rare problems are best solved that way in Python, anyway;-), but in almost every conceivable case of sentinel use a ''
sentinel will be just fine.