Python Write To File Missing Lines

python, python-3.x

Solution

Probably help if you called the method...

FILE.close()

Problem

I'm having trouble using python to write strings into a file: (what I'm trying to do is using python to generate some C programs) The code I have is the following: ``` filename = "test.txt" i = 0 string = "image" tempstr = "" average1 = "average" average2 = "average*average" output = "" FILE = open(filename,"w") while i < 20: j = 0 output = "square_sum = square_sum + " while j < 20: tempstr = string + "_" + str(i) + "_" + str(j) output = output + tempstr + "*" + tempstr + " + " + average2 + " - 2*" + average1 + "*" + tempstr if j != 19: output = output + " + " if j == 19: output = output + ";" j = j + 1 output = output + "\n" i = i + 1 print(output) FILE.writelines(output) FILE.close ``` The print gives me correct output, but the FILE has last line missing and some of the second last line missing. What's the problem in writing strings into file? Thank you!

Original source