Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 8 additions & 7 deletions Doc/library/functions.rst
Original file line number Diff line number Diff line change
Expand Up @@ -810,13 +810,14 @@ are always available. They are listed here in alphabetical order.

See also :ref:`typeiter`.

One useful application of the second form of :func:`iter` is to read lines of
a file until a certain line is reached. The following example reads a file
until the :meth:`~io.TextIOBase.readline` method returns an empty string::

with open('mydata.txt') as fp:
for line in iter(fp.readline, ''):
process_line(line)
One useful application of the second form of :func:`iter` is to build a
block-reader. For example, reading fixed-width blocks from a binary
database file until the end of file is reached::

from functools import partial
with open('mydata.db', 'rb') as f:
for block in iter(partial(f.read, 64), ''):
process_block(block)


.. function:: len(s)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Improve example of iter() with 2nd sentinel argument.