[issue46304] Unable to iterate over lines in a file without a block of code

Jason R. Coombs report at bugs.python.org
Sat Jan 8 15:42:45 EST 2022


Jason R. Coombs <jaraco at jaraco.com> added the comment:

Hi Eric. I did mention that option in my report, but that option requires loading the whole file into memory. I'd like something equivalent to the iterator that `open()` provides, which yields lines lazily.

Serihy, thanks for the feedback. I do indeed not want to rely on the implicit closing of the file handle. I'd instead like a helper function/method that will close the file after the iterator is consumed.

Something like:

def read_lines(path):
    with path.open() as strm:
        yield from strm

What I'm seeking is for that block to be placed somewhere in the stdlib so that I don't have to copy it into every project that needs/wants this behavior.

----------
type:  -> enhancement

_______________________________________
Python tracker <report at bugs.python.org>
<https://bugs.python.org/issue46304>
_______________________________________


More information about the Python-bugs-list mailing list