Append to file in Python

Open the file in append mode ‘a’. Write cursor points to the end of file. Then write to the file:

data_to_add = "Write a new line"
with open("data.txt", "a") as f:
  f.write(data_to_add + "\n")