I have Python code where I loop through every line in a file object with the typical syntax for line in file_object:
. This loop has another identical loop nested in it, also iterating over a file object. I was hoping that when the nested loop finishes, and a new iteration of the parent loop begins, the nested loop would restart from the beginning, but it does not. How can I make it reset with every iteration of the parent for loop?
Asked
Active
Viewed 34 times
0

Rohan
- 482
- 6
- 16
-
Briefly: with `file_object.seek(0)`. But I would recommend doing all the processing in a single pass over the file, or, it that's not possible (and the file isn't gigantic), saving it to something like a `list`. – TigerhawkT3 Jan 04 '16 at 04:46
-
Unfortunately, I don't have a good idea of making it possible to do a single pass over the file, and I have to account for the scenarios either files may be rather enormous, so I think `file_object_seek(0)` will be best. Thank you! – Rohan Jan 04 '16 at 04:55