Correct way to write line to file?

The correct way to write a line to a file in Python is to use the write() method on the file object, followed by the line you want to write. Here is an example:

with open("example.txt", "w") as file:
    file.write("This is a line in the file.")

Watch a course Python - The Practical Guide

You can also use the writelines() method to write multiple lines at once.

with open("example.txt", "w") as file:
    lines = ["This is the first line.\n", "This is the second line.\n"]
    file.writelines(lines)

Make sure to close the file after you finish writing by using 'with' statement, it automatically close the file after it's done writing.