Add new line to file python

A simple one!

In Python, you can add a new line to a file using the open function in write mode ('w') or append mode ('a'). Here are examples of both:

Write mode ('w'):

with open('file.txt', 'w') as f:
    f.write('Hello, world!\n')

This will overwrite the entire file with the new line.

Append mode ('a'):

with open('file.txt', 'a') as f:
    f.write('Hello, world!\n')

This will add the new line to the end of the file, without overwriting any existing content.

Note the use of the with statement, which ensures that the file is properly closed after writing, regardless of whether an exception is thrown or not.

Also, the \n at the end of the string is important, as it represents a newline character. If you don't include it, the new line will not be properly formatted.