New submission from Jason R. Coombs <[email protected]>:
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?
----------
components: Library (Lib)
messages: 410075
nosy: jaraco
priority: normal
severity: normal
status: open
title: Unable to iterate over lines in a file without a block of code
versions: Python 3.11
_______________________________________
Python tracker <[email protected]>
<https://bugs.python.org/issue46304>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe:
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com