我有一个大文件,我已经实现了图算法。程序没有问题。但是我必须将输出写入文件。我的输入将是一个数字,输出将是输入节点连接的所有节点。生成的文件相当大,大约4-5页后突然停止。我遇到并使用了flush()
功能,但无济于事。
我的文件写代码是:
djkpath =(path[i]," -> ",path[i+1], "[weight = ", g.weights[(path[i], path[i+1])],"]")
#print(path[i] -> path[i+1] [weight = g.weights[(path[i], path[i+1])]])
finaldjk = " ".join(map(str, djkpath))
fo.write(finaldjk)
fo.write('\n')
fo.write('\n')
fo.write("}")
fo.close()
fo.flush()
生成的文件看起来像(我刚刚包含了看起来不完整的文件的结尾):
150 -> 75 [weight = 12 ]
75 -> 45 [weight = 25 ]
85 -> 150 [weight = 9 ]
150 -> 55 [weight = 7 ]
55 -> 20 [weight = 17 ]
20 -> 120 [weight = 7 ]
120 -> 43 [weight