Concept: Writing files
A file handle can be used to write data to a file using the write() method. The file must be opened in a mode that allows writing, such as 'w', 'a', or 'r+'.
The write() method takes a single string as an argument and writes it to the file. The writelines() method can be used to write a list of strings to the file. Neither of these methods automatically add a newline character at the end of the string, so you must include it if you want to write multiple lines.
The encoding parameter specifies the encoding to use when writing the file. If not specified, the default encoding is used, which is platform-dependent (e.g., UTF-8 on most modern systems). A full list of supported encodings can be found in the Python documentation.
with open(file, mode="w", encoding=None) as file_handle:
# Write a single string to the file.
file_handle.write("Hello, World\n")
# Write a list of strings to the file.
file.file_handle.writelines(["Line 1", " Line 1, Part II\n", "Line 2"])