MutaPath.write_text(text, encoding=None, errors='strict', linesep='\n', append=False)[source]

Write the given text to this file.

The default behavior is to overwrite any existing file; to append instead, use the append=True keyword argument.

There are two differences between write_text() and write_bytes(): newline handling and Unicode handling. See below.

  • written. (text - str/bytes - The text to be) –

  • used. (encoding - str - The text encoding) –

  • errors. (errors - str - How to handle Unicode encoding) – Default is 'strict'. See help(unicode.encode) for the options. Ignored if text isn’t a Unicode string.

  • of (linesep - keyword argument - str/unicode - The sequence) – characters to be used to mark end-of-line. The default is os.linesep. Specify None to use newlines unmodified.

  • if (append - keyword argument - bool - Specifies what to do) – the file already exists (True: append to the end of it; False: overwrite it). The default is False.

— Newline handling.

write_text() converts all standard end-of-line sequences ('\n', '\r', and '\r\n') to your platform’s default end-of-line sequence (see os.linesep; on Windows, for example, the end-of-line marker is '\r\n').

To override the platform’s default, pass the linesep= keyword argument. To preserve the newlines as-is, pass linesep=None.

This handling applies to Unicode text and bytes, except with Unicode, additional non-ASCII newlines are recognized: \x85, \r\x85, and \u2028.

— Unicode

If text isn’t Unicode, then apart from newline handling, the bytes are written verbatim to the file. The encoding and errors arguments are not used and must be omitted.

If text is Unicode, it is first converted to bytes() using the specified encoding (or the default encoding if encoding isn’t specified). The errors argument applies only to this conversion.