0

Recently I bumped into some weird behaviour from comprehension generator in python

My code:

with open('test.txt') as file:
    lines = (line for line in file.read().splitlines())

for line in lines:
    print(line)
for line in lines:
    print(line)

However instead of two iteration over the same file content I only get one:

row1
row2
row3
...

where I would expect:

row1
row2
row3
...

row1
row2
row3
...

I could not find solution to this issue on other forums so if you know the answer I would appreciate the help :-)

EDIT: This question was posted due to my lack of understanding on how generators work. I was convinced that once I finish a loop over a generator I can do another one and another as many times as I want. I am deeply sorry for this question.

Krzychu
  • 83
  • 7
  • 1
    It's because you consume generator once and second loop has nothing to work with. At all using generator in that way brings no benefit, cause you read entire file content in memory anyway. – Olvin Roght Jun 11 '21 at 10:32
  • Also [this](https://stackoverflow.com/questions/26294912/read-multiple-times-lines-of-the-same-file-python) and [this](https://stackoverflow.com/questions/3906137/why-cant-i-call-read-twice-on-an-open-file) and [this](https://stackoverflow.com/questions/2106820/re-open-files-in-python) – He3lixxx Jun 11 '21 at 10:34
  • all that are correct answears to the original question tho now I'm curious if there is a way to loop twice over the same generator in any way or that would be impossible to do – Krzychu Jun 11 '21 at 11:30

1 Answers1

0

You Should use list comprehension instead of generators, by replacing parenthesis with square brackets, like

with open('test.txt') as file:
    lines = [line for line in file.read().splitlines()] # parenthesis replaced with square brackets

for line in lines:
    print(line)
for line in lines:
    print(line)
Mudasir Habib
  • 704
  • 7
  • 14
  • well yes but that creates a list not a generator and I don't want to use extra space nor spend extra time making sth I do not need. unless there is no workaround then I guess I will have to do it. :- – Krzychu Jun 11 '21 at 11:27