wxpython 中的文本编辑器无法打开保存的文件。文件保存为文本文件,但打开时出现以下错误
Error opening file
'charmap' codec can't decode byte 0x8d in position 5: charcter maps to <undefined>
用于打开文件的代码如下所示,
def DoOpenFile(self):
#wcd = 'All files (*)|*|Editor files (*.ef)|*.ef|'
wcd='Text files(*.txt)|*.txt|Plain Text files (*.txt)|*.txt'
dir = os.getcwd()
open_dlg = wx.FileDialog(self, message='Choose a file', defaultDir=dir, defaultFile='',
wildcard=wcd, style=wx.OPEN|wx.CHANGE_DIR)
if open_dlg.ShowModal() == wx.ID_OK:
path = open_dlg.GetPath()
try:
file = open(path, 'r')
text = file.read()
file.close()
if self.text.GetLastPosition():
self.text.Clear()
self.text.WriteText(text)
self.last_name_saved = path
self.statusbar.SetStatusText('', 1)
self.modify = False
self.SetTitle(window_title + path)
except IOError, error:
dlg = wx.MessageDialog(self, 'Error opening file' + str(error))
dlg.ShowModal()
except UnicodeDecodeError, error:
dlg = wx.MessageDialog(self, 'Error opening file\n' + str(error))
dlg.ShowModal()
open_dlg.Destroy()