For loop not working twice on the same file descriptor [duplicate]

Solution 1:

That is because your file pointer has reached the end of the file. So you need to point it back to the beginning of the file before your next iteration. Put this before your second loop:

fd.seek(0)

Check the tutorial on input/output here. The part about seek states:

To change the file object’s position, use f.seek(offset, from_what).

Solution 2:

The for loop works by calling fd.next() until it raises StopIteration. When you iterate through it the second time, the file has already finished. To get back to the beginning, use fd.seek(0)

Solution 3:

Because for lines in fd: will place the read pointer, file pointer or whatever it's called at the end of the file. Call fd.seek(0) in between your for loops