How to Append Text to a File in Python

You could open the file in a or a+ mode if you want to append text to a file.

destFile = r"C:\Test\Test.txt"
with open(destFile, 'a') as f:
    f.write("some appended text")

The code above appends the text some appended text next to the last character in the file. For example, if the file ends with this is the last sentence, then it becomes this is the last sentencesome appended text after appending.

new line in appending

If you prefer to add the text in the new line, you need to add carriage break \r\n after the appended text to guarantee the next appended text will added in the new line.

destFile = r"C:\Test\Test.txt"
with open(destFile, 'a') as f:
    f.write("the first appended text\r\n")
    f.write("the second appended text\r\n")
    f.write("the third appended text\r\n")