【问题标题】: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")

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

【问题讨论】:

    标签: python file-io


    【解决方案1】:

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

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

    【讨论】:

      猜你喜欢
      • 2016-03-01
      • 2021-05-21
      • 1970-01-01
      • 1970-01-01
      • 2011-07-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多