python文件查找

<pre name="code" class="python">#!/usr/bin/env python 
#-*- encoding:utf8 -*-
import wx 
import os,os.path,itertools
import re
import wmi
from threading import Thread
import sys
reload(sys)
sys.setdefaultencoding('utf8')

def getdiskpart():
    """返回电脑磁盘分区情况
    """
    diskpartlist=[]
    c = wmi.WMI()
    for physical_disk in c.Win32_DiskDrive():
        for partition in physical_disk.associators("Win32_DiskDriveToDiskPartition"):
            for logical_disk in partition.associators("Win32_LogicalDiskToPartition"):
                diskpartlist.append(logical_disk.Caption)
    return diskpartlist

#filenamelist=[]

class MenuEventFrame(wx.Frame): 
    def __init__(self, parent, id): 
        wx.Frame.__init__(self, parent, id, u'文件查找',size=(400,500))
        self.initUI()
        self.Center()
        #self.search_thread

    def initUI(self):
        self.SetSizeHintsSz( wx.DefaultSize, wx.DefaultSize )       
        fgSizer2 = wx.FlexGridSizer( 4, 2, 0, 0 )
        fgSizer2.AddGrowableCol( 1 )
        fgSizer2.AddGrowableRow( 3 )
        fgSizer2.SetFlexibleDirection( wx.BOTH )
        fgSizer2.SetNonFlexibleGrowMode( wx.FLEX_GROWMODE_SPECIFIED )
        
        fgSizer2.SetMinSize( wx.Size( 2,4 ) ) 
        self.m_staticText18 = wx.StaticText( self, wx.ID_ANY, u"路径", wx.DefaultPosition, wx.DefaultSize, 0 )
        self.m_staticText18.Wrap( -1 )
        fgSizer2.Add( self.m_staticText18, 0, wx.ALL, 5 )
        
        self.pathvalue = wx.TextCtrl( self, wx.ID_ANY, wx.EmptyString, wx.DefaultPosition, wx.DefaultSize, 0 )
        fgSizer2.Add( self.pathvalue, 0, wx.ALL|wx.EXPAND, 5 )
        
        self.m_staticText19 = wx.StaticText( self, wx.ID_ANY, u"文件格式", wx.DefaultPosition, wx.DefaultSize, 0 )
        self.m_staticText19.Wrap( -1 )
        fgSizer2.Add( self.m_staticText19, 0, wx.ALL, 5 )
        
        self.fromatvalue = wx.TextCtrl( self, wx.ID_ANY, wx.EmptyString, wx.DefaultPosition, wx.DefaultSize, 0 )
        fgSizer2.Add( self.fromatvalue, 0, wx.ALL|wx.EXPAND, 5 )
        
        self.m_staticText20 = wx.StaticText( self, wx.ID_ANY, u"", wx.DefaultPosition, wx.DefaultSize, 0 )
        self.m_staticText20.Wrap( -1 )
        fgSizer2.Add( self.m_staticText20, 0, wx.ALL, 5 )

        
        self.button = wx.Button( self, wx.ID_ANY, u"查询", wx.DefaultPosition, wx.DefaultSize, 0 )
        fgSizer2.Add( self.button, 0, wx.ALL|wx.ALIGN_RIGHT, 5 )
        
        self.m_staticText21 = wx.StaticText( self, wx.ID_ANY, u"结果", wx.DefaultPosition, wx.DefaultSize, 0 )
        self.m_staticText21.Wrap( -1 )
        fgSizer2.Add( self.m_staticText21, 0, wx.ALL, 5 )
        
        self.m_textCtrl8 = wx.TextCtrl( self, wx.ID_ANY, wx.EmptyString, wx.DefaultPosition, wx.DefaultSize, wx.HSCROLL|wx.TE_READONLY|wx.TE_MULTILINE )
        fgSizer2.Add( self.m_textCtrl8, 0, wx.ALL|wx.EXPAND, 5 )
        
        
        self.SetSizer( fgSizer2 )
        self.Layout()        
        self.Centre( wx.BOTH )
        self.Bind(wx.EVT_BUTTON,self.OnClick,self.button)

    def work_func(self,searchfile,path,value,textctrl):
        try:
            textctrl.Disable()
            self.button.Disable()
            searchfile(path,value,textctrl)   
        except Exception, e:
            raise
        finally:
            textctrl.Enable()
            self.button.Enable()

    def OnClick(self,event):
   
            self.m_textCtrl8.SetValue("")

            if self.fromatvalue.GetValue() == '':
                wx.MessageBox(u"请输入要查找的文件格式")
                return

            value=self.fromatvalue.GetValue().split()

            if self.pathvalue.GetValue()=='':
                for dirr in getdiskpart():
                    path=dirr
                    self.search_thread = Thread(target=self.work_func,args=(searchfile,path,value,self.m_textCtrl8))
                    self.search_thread.start()
            else:
                path=self.pathvalue.GetValue()
                if not os.path.isdir(path):
                  
                    wx.MessageBox(u'请输入正确的路径')
                    return
                self.search_thread = Thread(target=self.work_func,args=(searchfile,path,value,self.m_textCtrl8))
                self.search_thread.start()
               
def anyTrue(filename, sequence):
    return True in itertools.imap(filename.endswith, sequence)


def searchfile(path,endswithformate,textctrl):
    for basedir,dirlist,filenames in os.walk(path):
            for x in filenames:
                if anyTrue(x,endswithformate):
                    textctrl.WriteText(os.path.join(basedir,x)+os.linesep)



if __name__ == '__main__': 
    app = wx.App() 
    frame = MenuEventFrame(parent=None, id=-1) 
    frame.Show() 
    app.MainLoop() 















 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值