日期:2014-05-16  浏览次数:20674 次

文件内容的批量查找,wxPython[适合于win,linux]+编码问题
#! /usr/bin/env python
# -*- coding: utf-8 -*-
import wx,os,sys
out=None
recordsCount=0
suffixname="py"
mPattern=""
def listDir(path,isRecursive=0):
    global out,recordsCount,suffixname,mPattern
    if os.path.isfile(path):
        li=[path]
    else:
        li=os.listdir(path)
 #   print li
    for l in li:
        l = os.path.join(path, l)#传递绝对目录
        if os.path.isdir(l):
            if isRecursive==1:
                listDir(l,isRecursive);
            continue;
        if l[(l.rfind('.')+1):]!=suffixname:
            continue
       # print l
        os.getcwd()
        f=open(l)
        cnt=0;
        lineno=1
        try:
            for p in f.readlines():
                if p.decode('gbk').find(mPattern)>=0:#将读出来的内容由gbk解码成utf-8
                    if cnt==0:
                  #      print "[",l,"]"
                        out.write("[ %s ]\n" % l.encode('gb2312'))#对文件名进行编码再写入进去--因为windows下文件内容编码是gb2312
                    cnt=cnt+1
                 #   print lineno ,":", p.decode('gbk')#环境是utf8,读出的是gb2312
                    out.write("%d:%s\n" % (lineno,p))
                lineno=lineno+1
        except Exception,e:
            print e
        finally:
            f.close()
        recordsCount=recordsCount+cnt;
class MyFrame(wx.Frame):
    def __init__(self, *args, **kwds):
        # begin wxGlade: MyFrame.__init__
        kwds["style"] = wx.DEFAULT_FRAME_STYLE
        wx.Frame.__init__(self, *args, **kwds)
        self.text_ctrl_1 = wx.TextCtrl(self, -1, u"关键字")
        self.text_ctrl_2 = wx.TextCtrl(self, -1, u"文件后缀(py)")
        self.button_1 = wx.Button(self, -1, u"目录定位")
        self.check_1=wx.CheckBox(self,-1,u"递归查询")
        self.__set_properties()
        self.__do_layout()
        # end wxGlade
    def __set_properties(self):
        # begin wxGlade: MyFrame.__set_properties
        self.SetTitle(u"文件内容批量查找")
        self.button_1.SetBackgroundColour(wx.Colour(5, 255, 60))
        # end wxGlade

    def __do_layout(self):
        # begin wxGlade: MyFrame.__do_layout
        sizer_1 = wx.BoxSizer(wx.VERTICAL)
        grid_sizer_1 = wx.GridSizer(1, 4, 0, 0)
        grid_sizer_1.Add(self.text_ctrl_1, 0, wx.EXPAND, 0)
        grid_sizer_1.Add(self.text_ctrl_2, 1, wx.EXPAND, 0)
        grid_sizer_1.Add(self.check_1,2,wx.EXPAND,0)
        grid_sizer_1.Add(self.button_1, 0, 0, 0)
        sizer_1.Add(grid_sizer_1, 1, wx.EXPAND, 0)
        self.text_ctrl_1.Bind(wx.EVT_TEXT_ENTER,self.OnClick1)
        self.button_1.Bind(wx.EVT_LEFT_DOWN,self.OnClick1)
        self.SetSizer(sizer_1)
        sizer_1.Fit(self)
        self.Layout()
        # end wxGlade
    def OnClick1(self,event):
        if self.text_ctrl_1.Value==u"关键字":
            msgdlg=wx.MessageDialog(None,u"请先输入关键字",u"@_@",wx.ICON_QUESTION)
            msgdlg.ShowModal()
            msgdlg.Destroy()
            return
        if self.text_ctrl_2.Value==u"文件后缀(py)":
            msgdlg=wx.MessageDialog(None,u"请先输入文件后缀",u"@_@",wx.ICON_QUESTION)
            msgdlg.ShowModal()
            msgdlg.Destroy()
            return

        dlg=wx.DirDialog(None,u"目录定位",os.getcwd())
        if dlg.ShowModal()==wx.ID_OK:
            global out,recordsCount,suffixname,mPattern
            recordsCount=0
            out=open("results.txt","w")
            mPattern=self.text_ctrl_1.Value
            suffixname=self.text_ctrl_2.Value
            isRecursive=0
            if self.check_1.IsChecked():
                isRecursive=1
            try:
                #wx.MessageDialog(None,u"%d"%isRecursive,u"文件路径",wx.YES_NO|wx.ICON_QUESTION).ShowModal()
                listDir(dlg.GetPath(),isRecursive)
                out.write(u"found %d records" % reco