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

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).

Leave a Comment