As wim and I discussed in the comments, there is no advantage for this particular case. For the 2nd code snippet to be equivalent to the first code snippet then it would look something like this:
with open('mydata.txt') as fp:
for line in fp:
if line == '':
break
process_line(line)
However, the only case an empty string can be returned by readline
is at the end of the file (EOF) so it makes now difference here (other lines contain a newline '\n'
character at least).
If rather than an empty string another value was used, then the difference would be meaningful though. Personally, I think the docs should use a better example to illustrate this, like as follows:
>>> f = open('test')
>>> f.read()
'a\nb\nc\n\nd\ne\nf\n\n'
>>> f = open('test')
>>> [line for line in iter(f.readline, 'b\n')]
['a\n']
>>> f = open('test')
>>> [line for line in f]
['a\n', 'b\n', 'c\n', '\n', 'd\n', 'e\n', 'f\n', '\n']
(Note I should really be closing the file handles)
EDIT: I raised this as a possible documentation bug in issue34764