【问题标题】:Cannot open file using my text editor in wxpython?无法在 wxpython 中使用我的文本编辑器打开文件?
【发布时间】:2013-06-12 04:55:08
【问题描述】:

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()

【问题讨论】:

  • 你为什么不使用codecs.open()
  • 它确实成功了......

标签: python file-io wxpython


【解决方案1】:

把你的代码改成

 file = codecs.open(path, 'r',encoding='utf-8')

【讨论】:

    猜你喜欢
    • 2020-06-22
    • 2016-04-11
    • 2021-12-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-06-25
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多