Writing to a file in a for loop,myfile.close()