On Sun, 15 Dec 2019 at 05:54, David Mertz <me...@gnosis.cx> wrote:
>
> A pattern I've written a number of times is roughly:
>
> lines = open(fname)
> header = next(lines)
> for line in lines:
>     process (line, header)
>
> That's not so artificial, I think. Of course, first() would also work here. 
> But I'm not sure it's any particular advantage in this case.

I think you haven't understood the point about wanting a different
exception instead of StopIteration. This is precisely an example of
what not to do because it can fail in strange ways if the file is
empty (see above in this thread or PEP 479).

The simplest way to fix this is with 2-arg next:

with open(fname) as lines:
    header = next(lines, None)
    if header is None:
        # Not sure what you want to do here...
    for line in lines:
        process(line, header)

--
Oscar
_______________________________________________
Python-ideas mailing list -- python-ideas@python.org
To unsubscribe send an email to python-ideas-le...@python.org
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at 
https://mail.python.org/archives/list/python-ideas@python.org/message/6LCVRR6LXJEAV4AECSVKIICHK4VRGANI/
Code of Conduct: http://python.org/psf/codeofconduct/

Reply via email to