Writing string to a file on a new line every time

Here is an example code snippet for writing a string to a file on a new line every time in Python:

with open("example.txt", "a") as file:
    file.write("This is a new line\n")

The open function is used to open a file and the "a" argument specifies that the file should be opened in "append" mode. This means that any new data written to the file will be added to the end of the file, rather than overwriting the existing data.

Watch a course Python - The Practical Guide

The write method is used to write the string "This is a new line" to the file, followed by a newline character (\n) which will add a new line after the string.

The with open statement is used to ensure that the file is closed properly after the block of code is executed.