How to write to a file without overwriting current contents?

PythonFile Io

Python Problem Overview


with open("games.txt", "w") as text_file:
    print(driver.current_url)
    text_file.write(driver.current_url + "\n")

I'm using this code right now, but when it writes to the file it overwrites the old content. How can I simply add to it without erasing the content that's already there.

Python Solutions


Solution 1 - Python

Instead of "w" use "a" (append) mode with open function:

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionuser2540748View Question on Stackoverflow
Solution 1 - PythonPaulo BuView Answer on Stackoverflow