用wxpython写了个简单的文件转码工具

最近换的电脑是英文的系统,以前的一些资料都是中文gbk编码。导致一直乱码,系统没有管理员权限也不能修改编码

想想很久没用wxpythonl。就写了这样的一个小工具

 

#! /usr/bin/python
# -*- coding:utf-8 -*-
'''
Created on Jun 9, 2011

@author: eruan
'''

import wx
import sys
import os
import codecs
import shutil



_support_encoding = ['GBK', 'GB2312', 'UTF-8', 'Shift-JIS']

_system_encoding = sys.stdin.encoding

_support_wildcard = "txt(*.txt)|*.txt|sql(*.sql)|*.sql|cue(*.cue)|*.cue|All files (*.*)|*.*"


class MainFrame(wx.Frame):
    
    def __init__(self, parent, id, title, pos, size, style):
        
        wx.Frame.__init__(self, parent, id, title, pos=pos, size=size, style=style)
        
        self.DoLayout()
        self.file_list = set()
        self.file_success_list = set()


        # Event Bound
        self.choose_path.Bind(wx.EVT_BUTTON, self.OnChoosePath)
        self.begin_convert.Bind(wx.EVT_BUTTON, self.OnBeginConvert)
        self.deleteBackup.Bind(wx.EVT_BUTTON, self.OnDeleteBackup)
        
        
    def DoLayout(self):
        self.panel_up = wx.Panel(self)
        self.panel_down = wx.Panel(self)
        self.panel_up.SetBackgroundColour('#8f5049')
        
        
        grid = wx.GridBagSizer(vgap=10, hgap=10)
        
        
        self.choose_path = wx.Button(self.panel_up, label=u"选择文件", size=(200, 50))
        grid.Add(self.choose_path, (1, 1))
        
        
        self.is_backup = wx.CheckBox(self.panel_up, label=u'备份', size=(150, 50), style=wx.SHAPED)
        grid.Add(self.is_backup, (1, 2))
        
        source_encoding_text = wx.StaticText(self.panel_up, label=u'源文件编码:')
        self.source_encoding = wx.Choice(self.panel_up, choices=_support_encoding, size=(100, 30))
        source_encoding_vbox = wx.BoxSizer()
        source_encoding_vbox.Add(source_encoding_text, 1)
        source_encoding_vbox.Add(self.source_encoding, 1)
        grid.Add(source_encoding_vbox, (2, 1))
        
        dest_encoding_text = wx.StaticText(self.panel_up, label=u'目标文件编码:')
        self.dest_encoding = wx.Choice(self.panel_up, choices=_support_encoding, size=(100, 30))
        self.dest_encoding.SetStringSelection(_system_encoding)
        dest_encoding_vbox = wx.BoxSizer()
        dest_encoding_vbox.Add(dest_encoding_text, 1)
        dest_encoding_vbox.Add(self.dest_encoding, 1)
        grid.Add(dest_encoding_vbox, (2, 2))
        

        
        self.begin_convert = wx.Button(self.panel_up, label=u'开始转换', size=(200, 50))
        self.deleteBackup = wx.Button(self.panel_up, label=u'删除备份文件', size=(200, 50))
        self.deleteBackup.Disable()

        grid.Add(self.begin_convert, (3, 1))
        grid.Add(self.deleteBackup, (3, 2))
        self.panel_up.SetSizer(grid)
        
        
        self.file_list_info = wx.TextCtrl(self.panel_down, -1, '', size=(490, 200), style=wx.TE_MULTILINE | wx.TE_READONLY | wx.TE_LEFT)
        self.convert_info = wx.TextCtrl(self.panel_down, -1, '', size=(490, 250), style=wx.TE_MULTILINE | wx.TE_READONLY | wx.TE_LEFT)
        self.convert_info.SetBackgroundColour('#4f5049')
        vbox_info = wx.BoxSizer(wx.VERTICAL)
        vbox_info.Add(self.file_list_info, 1, wx.EXPAND)
        vbox_info.Add(self.convert_info, 1, wx.EXPAND)
        self.panel_down.SetSizer(vbox_info)
        
        

        
        
        vbox = wx.BoxSizer(wx.VERTICAL)
        vbox.Add(self.panel_up, 1, wx.EXPAND)
        vbox.Add(self.panel_down, 2, wx.EXPAND)
        
        
        self.SetSizer(vbox)
        
        
    def OnChoosePath(self, event):
        fileDialog = wx.FileDialog (self, u'选择文件夹', wildcard=_support_wildcard, style=wx.FD_OPEN | wx.FD_MULTIPLE)
        if fileDialog.ShowModal() == wx.ID_OK:
            self.file_list.clear()
            self.file_list_info.Clear()
            files = fileDialog.GetPaths()
            for file in files:
                self.file_list.add(file)
                self.file_list_info.AppendText(file + '\n')
            
                
    def OnBeginConvert(self, event):
        if not self.file_list:
            wx.MessageBox(u'请选择符合条件的文件',
                                  u'选择文件夹', style=wx.OK | wx.ICON_EXCLAMATION)
            self.OnChoosePath(event)
        elif self.source_encoding.GetSelection() == wx.NOT_FOUND:
            wx.MessageBox(u'请选择源文件编码',
                                  u'选择源文件编码', style=wx.OK | wx.ICON_EXCLAMATION)
            self.source_encoding.SetFocus()
        elif self.source_encoding.GetSelection() == wx.NOT_FOUND:
            wx.MessageBox(u'请选择目标文件编码',
                                  u'选择目标文件编码', style=wx.OK | wx.ICON_EXCLAMATION)
            self.source_encoding.SetFocus()
        else:
            source_encode = _support_encoding[self.source_encoding.GetSelection()].decode(_system_encoding)
            dest_encode = _support_encoding[self.dest_encoding.GetSelection()].decode(_system_encoding)
            self.convert_info.Clear()
            for file_name in self.file_list:
                try:
                    with codecs.open(file_name, 'r', source_encode) as source_file:
                        context = source_file.read()
                except UnicodeDecodeError , e:
                    self.convert_info.AppendText('文件 %s 转换失败!请尝试其他编码' % file_name + '\n')
                else:
                    #back up file
                    if self.is_backup.IsChecked():
                        shutil.copy(file_name, file_name + '.bak')
                    dest_file = open(file_name, 'w')
                    dest_file.write(context.decode(dest_encode))
                    dest_file.close()
                    self.convert_info.AppendText('文件 %s 转换成功!' % file_name + '\n')
                    self.file_success_list.add(file_name)
            if self.file_success_list and self.is_backup.IsChecked():
                self.deleteBackup.Enable() 
                    
                    
    def OnDeleteBackup(self, event):
            self.convert_info.Clear()
            for file_name in self.file_success_list:
                backup_file_name = file_name + '.bak'
                os.remove(backup_file_name)
                self.convert_info.AppendText('删除 %s 成功 \n' % backup_file_name)
            wx.MessageBox(u'删除备份文件成功',
                              u'信息', style=wx.OK | wx.ICON_EXCLAMATION)
            self.deleteBackup.Disable()
            self.file_success_list.clear()
            
            
class MainApp(wx.App):
    
    def OnInit(self):
        screen_x = (wx.DisplaySize()[0] - 500) / 2
        screen_y = (wx.DisplaySize()[1] - 600) / 2
        self.frame = MainFrame(None, -1, u'文件转换器', (screen_x, screen_y), (500, 600), wx.DEFAULT_FRAME_STYLE ^ wx.MAXIMIZE_BOX)
        font = wx.SystemSettings.GetFont(wx.SYS_DEFAULT_GUI_FONT)
        self.frame.SetFont(font)
        self.frame.Show()
        return True
        
  

if __name__ == '__main__':
    app = MainApp(False)
    app.MainLoop()
    




    


 

 截图

用wxpython写了个简单的文件转码工具

 

 

现在只支持文件夹和符合条件的文件的检查,遍历方式 在只选根目录的情况下,性能很差,有卡住的情形,明天好好的看下,在加入文件选择的方式

 

 

 

 

 

你可能感兴趣的:(sql,python,OS,UP,wxPython)