python中的with open和 open创建文件(txt)的区别

起因

在python写入多行时,发现用open的方式写不完,肯定是循环到了,就是到了某一固定行就不继续写了,将文件名换成txt、xls、dat等格式结果均相同,后来改为with open 的方式创建文件名,就记录全了。原因不明。

open示例

wav_txt = open('list.txt', 'w')
for gf in range(len(file_names)):
	wav_txt.write('%s\t%d\n' %(file_names[gf], gf))

写入不完全。

with open示例

with open('list.dat', 'a', encoding='utf-8') as f:
        for i in index:
            f.write('%d\t%s\t%d\n' % (i, Dzone[i], flag[i]))
        f.close()

可以完整记录。

Guess you like

Origin blog.csdn.net/u011913417/article/details/117528701