Opening a file in append mode places the next write at the end.
with open("votes.txt", "w", encoding="utf-8") as file:
file.write("yes")
with open("votes.txt", "a", encoding="utf-8") as file:
file.write(",no")
with open("votes.txt", encoding="utf-8") as file:
print(file.read()) # yes,noThe append call preserved the first vote. A line-based log needs a newline after every new entry.
Implement append_log(path, entries) for a UTF-8 log. Append each entry followed by a newline. An existing log is empty or ends with a newline. Create the file if missing, even if the new list is empty. Entries contain no newlines.
append_log("log.txt", ["reopened", "ready"]) # appends "reopened\nready\n"