如何在不覆盖当前内容的情况下写入文件? [复制]

Posted

技术标签:

【中文标题】如何在不覆盖当前内容的情况下写入文件? [复制]【英文标题】:How to write to a file without overwriting current contents? [duplicate] 【发布时间】:2014-04-21 22:00:03 【问题描述】:
with open("games.txt", "w") as text_file:
    print(driver.current_url)
    text_file.write(driver.current_url + "\n")

我现在正在使用此代码,但是当它写入文件时会覆盖旧内容。我怎样才能简单地添加它而不删除已经存在的内容。

【问题讨论】:

【参考方案1】:

使用"a"(附加)模式和open 函数代替"w"

with open("games.txt", "a") as text_file:

【讨论】:

以上是关于如何在不覆盖当前内容的情况下写入文件? [复制]的主要内容,如果未能解决你的问题,请参考以下文章