Well, the problem is that you open the file (which currently clears it's contents), write one line, close the file, iterate to the next ban, open the file again (clearing it again), and so forth...
Move the open and close commands outside the loop, and it should work like a charm...