Python not writing full string to file

帅比萌擦擦* 提交于 2019-11-29 13:54:08

Are you closing the file at the end? It may be that some of your data sits in the buffer, so it is good to close() the file when you are done (or flush() it if you don't want to close it for some reason).

Better yet, to use the with construct which will close the file for you when you are done or an exception occurs:

with open('out.7.9.12.txt','a+') as out:
   # the rest of your code indented under here
   # ....
   score_dict={}
   Max=245

Note from the flush() docs:

Note flush() does not necessarily write the file’s data to disk. Use flush() followed by os.fsync() to ensure this behavior

Use out.flush() at the end (or close() ) to clear buffers.

Try putting out.close() at the end of your script. It will ensure that all your output gets flushed to the file.

Also, 'a+' is redundant; 'a' is append-mode which is already writeable.

Max = '245'
al1 = 'XMXXXXXXXXXXXXXXXXXXX ...'
scores = {Max:[al1]}

with open('out.7.9.12.txt', 'a') as outf:
    for name,data in scores.iteritems():
        outf.write('>{}\n{}\n'.format(name, '\n'.join(data)))

I was having this exact issue. I found that I neglected to call close as a method. I was using foo.close instead of foo.close().

That resolved it.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!