1

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

asked Sep 3, 2011 at 19:57

3 Answers 3

2

Close the file when done with all the writes to ensure any write-caching is flushed to the drive with:

writewtsi.close()
answered Sep 3, 2011 at 20:44
Sign up to request clarification or add additional context in comments.

1 Comment

Yes, that's it. both flush() and close() do the work well. Thanks a lot.
0

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] 
answered Sep 3, 2011 at 20:20

1 Comment

Yes, that's it. both flush() and close() do the work well. Thanks a lot.
0

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]
answered Sep 3, 2011 at 21:25

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.