I want to save list to a file so I cycle through it and write it to file. Everything's fine. But SOMETIMES(!?!?) the list is not written entirely, it stops rendering in the middle of the item. No error is raised, it silently continues executing rest of the code. I've tried several ways to write 开发者_开发问答it out, several versions of python (2.4, 2.5, 2.7) and it's all the same. It sometimes work, sometimes not. When it's printed out to the terminal window, not to the file, it's working properly without glitches. Am I missing something?
this is it
...
from bpnn import *
...
# save input weights for later use:
writewtsi = open("c:/files/wtsi.txt", "w")
for i in range(net.ni):
print>>writewtsi, net.wi[i]
bpnn is neural network module from here: http://python.ca/nas/python/bpnn.py
Close the file when done with all the writes to ensure any write-caching is flushed to the drive with:
writewtsi.close()
Does the problem persist if you use:
with open("c:/files/wtsi.txt", "w") as writewtsi:
for i in range(net.ni):
print>>writewtsi, net.wi[i]
use
.flush()
like so:
...
from bpnn import *
...
# save input weights for later use:
writewtsi = open("c:/files/wtsi.txt", "w")
for i in range(net.ni):
print>>writewtsi, net.wi[i]
writewtsi.flush()
Or you could make the file unbuffered with the 3rd parameter to open():
...
from bpnn import *
...
# save input weights for later use:
writewtsi = open("c:/files/wtsi.txt", "w", 0)
for i in range(net.ni):
print>>writewtsi, net.wi[i]
精彩评论