2013-06-12 44 views
0

wxpython中的文本編輯器無法打開保存的文件。該文件保存爲文本文件,但在打開下面的錯誤出現無法在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() 
+1

你爲什麼不使用'codecs.open()'? –

+0

它實際上做了詭計...... – chris

回答

1

更改您的代碼

file = codecs.open(path, 'r',encoding='utf-8') 
相關問題