You must add a parameter to the open() function: "w" - Write - will overwrite any existing content Example:
data_to_add = "Ups! I've overwritten the file!" with open("data.txt", "w") as f: f.write(data_to_add + "\n")