To replace the content completely, we use the "r"
mode, so we pass this string as the second argument to open()
. We call the .write()
method on the file object passing the content that we want to write as argument.
For example:
words = ["Blue", "Amazing", "Python", "Code"]
with open("famous_places.txt", "r") as file:
for word in words:
file.write(word + "\n")
[mai mult...]