mutapath.MutaPath.write_text

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.

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

  • - str - The Unicode encoding that will be used. (encoding) – This is ignored if text isn’t a Unicode string.

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

  • - keyword argument - str/unicode - The sequence of (linesep) – characters to be used to mark end-of-line. The default is os.linesep. You can also specify None to leave all newlines as they are in text.

  • - keyword argument - bool - Specifies what to do if (append) – 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').

If you don’t like your platform’s default, you can override it using the linesep= keyword argument. If you specifically want write_text() to preserve the newlines as-is, use linesep=None.

This applies to Unicode text the same as to 8-bit text, except there are three additional standard Unicode end-of-line sequences: u'\x85', u'\r\x85', and u'\u2028'.

(This is slightly different from when you open a file for writing with fopen(filename, "w") in C or open(filename, 'w') in Python.)

— 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.