gpt4 book ai didi

Python。 While循环附加txt文件名和新文件名

转载 作者:太空宇宙 更新时间:2023-11-03 15:21:56 25 4
gpt4 key购买 nike

嗨,我正在尝试使用 while 循环来格式化 txt 文件 {0} + 1,而每次我执行 python source.py 时,它都会附加 + 1 并且不会重置回 0 和重写 7 个 txt 文件输出。所以下一次执行将在它停止的地方 + 1,我不太确定该怎么做?有什么帮助吗?

    i = 0
while i < 7:
i += 1
random.shuffle(chunks)
output = ' '.join(chunks)
f = open("c:\\workspace\\simple\\{0}.txt".format(i), 'w')
f.write(output)
f.close()
if i == 7:
break

最佳答案

你应该改变这个

f = open("c:\\workspace\\simple\\{0}.txt".format(i), 'w')

对此:

f = = open("c:\\workspace\\simple\\{0}.txt".format(i), 'a')

a 将向文件添加一行

关于Python。 While循环附加txt文件名和新文件名,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43462137/

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