gpt4 book ai didi

Python 写入文件缺失行

转载 作者:太空宇宙 更新时间:2023-11-04 06:50:54 28 4
gpt4 key购买 nike

我在使用 python 将字符串写入文件时遇到问题:(我想做的是使用 python 生成一些 C 程序)我的代码如下:

filename = "test.txt"
i = 0
string = "image"
tempstr = ""
average1 = "average"
average2 = "average*average"
output = ""
FILE = open(filename,"w")
while i < 20:
j = 0
output = "square_sum = square_sum + "
while j < 20:
tempstr = string + "_" + str(i) + "_" + str(j)
output = output + tempstr + "*" + tempstr + " + " + average2 + " - 2*" + average1 + "*" + tempstr
if j != 19:
output = output + " + "
if j == 19:
output = output + ";"
j = j + 1
output = output + "\n"
i = i + 1
print(output)
FILE.writelines(output)
FILE.close

打印给了我正确的输出,但是 FILE 缺少最后一行并且缺少一些倒数第二行。将字符串写入文件有什么问题?

谢谢!

最佳答案

如果您调用该方法,可能会有所帮助...

FILE.close()

关于Python 写入文件缺失行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11199569/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com