展开全部
py2.7,需要调用codecs模块来进行设置#coding:utf-8
import sys
reload(sys)
sys.setdefaultencoding('utf-8')
import traceback
import os
#含有中文时,最好用codecs打开
import codecs
filepath = os.getcwd()+'/testfile.txt'
try:
with codecs.open(filepath,'w+','utf-8') as f:
seq =[
'heheheheheheheheheheheh',
'hohohohohohohohohohohoho',
'hahahahahahahahahaha',
'这里又32313133353236313431303231363533e78988e69d8331333363383339是哪里\n']
f.write("********************\n")
f.writelines(seq)
with codecs.open(filepath,'r','utf-8') as f:
for i in f.readlines():
print i
except Exception,e:
traceback.print_exc()
print "执行文件时报错:"+str(e)
py3.6的open函数