我正在尝试将脚本的结果输出到文本文件中。该脚本工作正常,唯一的问题是当结果保存到文本文件(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
Run Code Online (Sandbox Code Playgroud) 我读到 python 中的赋值不会像在 c 中那样复制它,它会为对象分配一个指针。
但是当我调试这个函数时:
def popall(self):
objs = self.curstack
self.curstack = []
return objs
Run Code Online (Sandbox Code Playgroud)
看起来正在进行某种复制。这个函数运行后obis满是东西,self.curstack
是空的……
所以一些副本正在进行中。它是深的还是浅的?