Sunday, 4 February 2018

Python write to file without overwriting current txt




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.


Answer




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



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

No comments:

Post a Comment

casting - Why wasn't Tobey Maguire in The Amazing Spider-Man? - Movies & TV

In the Spider-Man franchise, Tobey Maguire is an outstanding performer as a Spider-Man and also reprised his role in the sequels Spider-Man...