# coding=utf-8 ##以utf-8编码储存中文字符
import os
import codecs
path = "d:/Python/c.txt"
try:
f=codecs.open(path,'w', 'UTF-8')
f.close()
except Exception as e:
print(e)
os.system('pause')
Python 3.6.1
The above code can only create text files in ANSI format. How to create UTF-8 files?
In fact, the code of the subject can create UTF-8 files, but because there is no content written in the file, the empty txt file does not have encoding. Write some UTF characters and try again and it will be OK
Open the c.txt file again and it will be UTF-8.
(Python3.4)
encoding='utf8'