I want to save the output (contents) to a file (saving it in UTF-8). The file shouldn’t be overwritten, it should be saved as a new file – e.g. file2.txt
So, I fists open a file.txt, encode it in UTF-8, do some stuff and then wanna save it to file2.txt in UTF-8. How do I do this?
import codecs
def openfile(filename):
with codecs.open(filename, encoding="UTF-8") as F:
contents = F.read()
...
The short way:
The long way:
And using ‘codecs’ explicitly: