Write to a file opened in text mode.
(file_path, text, line_endings=None)
| 179 | |
| 180 | |
| 181 | def write_file(file_path, text, line_endings=None): |
| 182 | """Write to a file opened in text mode.""" |
| 183 | if line_endings and line_endings != os.linesep: |
| 184 | text = text.replace('\n', line_endings) |
| 185 | write_binary(file_path, text.encode('utf-8')) |
| 186 | else: |
| 187 | with open(file_path, 'w', encoding='utf-8') as fh: |
| 188 | fh.write(text) |
| 189 | |
| 190 | |
| 191 | def write_binary(file_path, contents): |