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

2022-01-09 Thread Serhiy Storchaka
Serhiy Storchaka added the comment: The safe way of using read_lines() is: lines = read_lines() try: # use lines finally: lines.close() or with contextlib.closing(read_lines()) as lines: # use lines And it is in no way better than using "with open()"

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

2022-01-08 Thread Eric V. Smith
Eric V. Smith added the comment: I can't believe I missed that, Jason. I even read it twice! I think this could go in pathlib, along with read_text. Maybe read_lines, or iter_lines, or something. Of course PEP 533 is needed, too. -- ___ Python tra

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

2022-01-08 Thread Jason R. Coombs
Jason R. Coombs added the comment: Nice reference. Indeed, the [rationale](https://www.python.org/dev/peps/pep-0533/#id15) of that pep gives a similar example and the [background](https://www.python.org/dev/peps/pep-0533/#id3) describes the problem with the solution I've drafted above (it s

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

2022-01-08 Thread Dennis Sweeney
Dennis Sweeney added the comment: I think this might require something like PEP 533 in order to be safe. -- nosy: +Dennis Sweeney ___ Python tracker ___ __

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

2022-01-08 Thread Jason R. Coombs
Jason R. Coombs 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 w

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

2022-01-08 Thread Serhiy Storchaka
Serhiy Storchaka added the comment: A warning is an indication of possible bugs in your code. If you do not close file explicitly, and it is closed by the garbage collector, the time of closing is undeterminated. This can lead to exhausting of file descriptors if you have a lot of opened fil

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

2022-01-07 Thread Eric V. Smith
Eric V. Smith added the comment: Hi, Jason. How about: >>> from pathlib import Path >>> Path("foo.txt").read_text().splitlines() ['how', 'now', 'brown', 'cow'] Not the most elegant thing, I'll admit. -- nosy: +eric.smith ___ Python tracker

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

2022-01-07 Thread Jason R. Coombs
New submission from Jason R. Coombs : 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: ``` $