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