gpt4 book ai didi

Python - 只有最后一行被保存到文件

转载 作者:行者123 更新时间:2023-12-05 09:22:41 25 4
gpt4 key购买 nike

我正在尝试将我的脚本的结果输出到一个文本文件中。脚本工作正常,唯一的问题是当结果保存到文本文件(output.txt)时,只保存最后一行,而不是整个文件?我不确定我在这里做错了什么。任何建议将不胜感激。

加油!

        try:

if 'notavailable' not in requests.get('url' + str(service) + '&username=' + str(username), headers={'X-Requested-With': 'XMLHttpRequest'}).text:
result = service + '\t' + " > " + username + " > " 'Available'
print result
f = open("output.txt", "w")
f.write(result + "\n")
f.close()

else:
print service + '\t' + " > " + username + " > " 'Not Available'

except Exception as e:
print e

最佳答案

你需要写

f = open("output.txt", "a")

这将附加文件,而不是覆盖您放入的任何其他内容。

关于Python - 只有最后一行被保存到文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25115140/

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