Message410075
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? |
|
Date |
User |
Action |
Args |
2022-01-08 04:09:44 | jaraco | set | recipients:
+ jaraco |
2022-01-08 04:09:44 | jaraco | set | messageid: <1641614984.33.0.0710604496782.issue46304@roundup.psfhosted.org> |
2022-01-08 04:09:44 | jaraco | link | issue46304 messages |
2022-01-08 04:09:44 | jaraco | create | |
|