This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Author jaraco
Recipients jaraco
Date 2022-01-08.04:09:44
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1641614984.33.0.0710604496782.issue46304@roundup.psfhosted.org>
In-reply-to
Content
I'd like to be able to do something pretty fundamental: lazily load lines from a file in a single expression.

Best I can tell, that's not possible in the language without triggering warnings.

One can use 'open' but that triggers a ResourceWarning:

```
$ $PYTHONWARNINGS='error' python -c "lines = open('/dev/null'); tuple(lines)"
Exception ignored in: <_io.FileIO name='/dev/null' mode='rb' closefd=True>
ResourceWarning: unclosed file <_io.TextIOWrapper name='/dev/null' mode='r' encoding='UTF-8'>
```

One can use a `with` statement, but that requires a block of code and can't be written easily in a single expression. One can use `pathlib.Path.read_text().splitlines()`, but that loads the whole file into memory.

This issue affected the pip-run project, which required 5 new lines in order to make such an expression possible (https://github.com/jaraco/pip-run/commit/e2f395d8814539e1da467ac09295922d8ccaf14d).

Can the standard library supply a function or method that would provide this behavior?
History
Date User Action Args
2022-01-08 04:09:44jaracosetrecipients: + jaraco
2022-01-08 04:09:44jaracosetmessageid: <1641614984.33.0.0710604496782.issue46304@roundup.psfhosted.org>
2022-01-08 04:09:44jaracolinkissue46304 messages
2022-01-08 04:09:44jaracocreate