I have a simple question which I stock in it !
for line1 in file:
print(line1)
for line2 in file:
print(line2)
for line3 in file:
print(line3)
I expect this to work three times, but only "for line1 in file:" work.
I have a simple question which I stock in it !
for line1 in file:
print(line1)
for line2 in file:
print(line2)
for line3 in file:
print(line3)
I expect this to work three times, but only "for line1 in file:" work.
Because the file is read as part of iterating over the lines. You'll need to reopen the file each time, or read the whole file into a list of lines (via file.readlines() perhaps) and iterate over that, if memory limits permit. Any open file has a "read pointer" that tracks what's been read, which advances with each line consumed. The loops as written will each consume the whole file.