def open_writer(self, filename, encoding="mbcs"):
# A place to put code to open a file with the appropriate encoding.
# Does *not* set self.file - just opens and returns a file.
# Actually *deletes* the filename asked for and returns a handle to a
# temp file - finish_writer then puts everything back in place. This
# is so errors don't leave a 1/2 generated file around causing bizarre
# errors later.
# Could be a classmethod one day...
try:
os.unlink(filename)
except os.error:
pass
filename = filename + ".temp"
if sys.version_info > (3,0):
ret = open(filename, "wt", encoding=encoding)
else:
import codecs # not available in py3k.
ret = codecs.open(filename, "w", encoding)
return ret
评论列表
文章目录