I have a list (List) of 4196 elements, all equal to either -1 or 1. I want to export the list to a .txt file. Here’s the code I used:
file = open('file.txt','w')
for item in List:
print>>file, item
For some reason, the .txt file only has 2870 elements. (The same thing happened when I tried another way of exporting the list, but I know there are 4196 elements!)
Thanks for any help,
Zach
You need to close the file. You can’t easily tell from the number of lines, but I’d expect the size of the file to be 4096 or 8192, which hints that only a whole number of blocks have been flushed. After you call
file.close(), the rest of the data should be written.You can use a
withstatement to close the file automatically: