python寫的本地搜尋小工具
阿新 • • 發佈:2018-12-23
#coding=utf-8 #small search tool by tkinter #test plat form: Windows XP Chinese #version 0.5 #file name: find.py #author: zhouhh #date: 2008.12.16 #email:ablozhou gmail.com #note:to search files modified a few days ago #debug tool: PythonWin #python version: 2.5.2 import os; import time; from Tkinter import * class capp: ''''' 對輸入的路徑和檔案字尾以及編輯的天數進行搜尋,並在輸入路徑下生成一個csv結果檔案。''' def __init__(self,master): ''''' file search tool 0.5 by
[email protected] 周海漢 ''' frame1 = Frame(master) frame1.pack() self.quest = Label(frame1,bitmap='questhead') self.quest.pack(side=LEFT) #self.button = Button(frame,text='Quit',fg='red',command=frame.quit) #self.button.pack(side=LEFT) self.dirlable=Label(frame1,text=unicode('輸入搜尋路徑: ','gbk'),font=('songti',9)) self.dirlable.pack(side=LEFT) self.entry = Entry(frame1,font=('songti',9),width=65) self.entry.pack(side=LEFT) frame = Frame(master) frame.pack() self.lblext=Label(frame,text=unicode(' 檔案字尾名: ','gbk'),font=('songti',9)) self.lblext.pack(side=LEFT) self.ext = Entry(frame,width=15) self.ext.pack(side=LEFT) self.extstr=StringVar() self.extstr.set(u'*.xls') self.ext['textvariable']=self.extstr self.contents = StringVar() self.contents.set(u'') self.entry['textvariable']=self.contents self.daylable=Label(frame,text=unicode(' 修改時間(幾天前):','gbk'),font=('songti',9)) self.daylable.pack(side=LEFT) self.days = Entry(frame,width=15) self.days.pack(side=LEFT) self.daystr=StringVar() self.daystr.set('1') self.days['textvariable']=self.daystr self.hello = Button(frame,text=unicode('查詢','gbk'),font=('songti',12,'bold'),fg='red',bg='white',height=2,width=8,command=self.sayhi) self.hello.pack(side=LEFT) frame3 = Frame(master) frame3.pack() self.msg = Label(frame3,text=unicode('','gbk')) self.msg.pack(side=LEFT) def sayhi(self): print 'begining ... ' self.msg.config(text='...') try: mydir = self.contents.get() mydays = self.daystr.get() myext =self.extstr.get() file1 = open(os.path.join(mydir,'result.csv'),'a+') searchinfo = 'nsearch '+myext+' in '+mydays+' days. seach time:'+time.strftime('%Y-%m-%d %H:%M:%S',time.localtime(time.time()))+' n' file1.write(searchinfo) file1.write('File name, Modify time, Size (bytes), Directoryn') self.listfile(mydir,file1,mydays,myext) file1.close() self.msg.config(text=unicode('搜尋成功!','gbk'),font=('songti',9)) except IOError,(errno,strerror): print 'IO error %s: %s' % (errno,strerror) self.msg.config(text = 'IO error %s: %s' % (errno,strerror)) except: print "Unexpected error:", sys.exc_info()[0] self.msg.config(text='error '+str(sys.exc_info()[0])) file1.close() raise def listfile(self,dirname,file1,days,ext): ''''' search files ... ''' if len(ext) >0 : ext = os.path.splitext(ext)[1] if len(dirname) > 0: os.chdir(dirname) dirname = os.getcwd() print '['+dirname+']:' names = os.listdir(dirname) dirs=[] for filename in names: fullname = os.path.join(dirname,filename) if os.path.isdir(fullname): dirs.append(fullname) continue # if len(ext) > 0: if os.path.splitext(filename)[1]!=ext : status = 'ignore '+filename+' for extension' print status self.msg.config(text=unicode(status,'gbk'),font=('songti',9)) continue; t = os.path.getmtime(fullname) tnow = time.time() #print fullname,' ignore',tnow,t,tnow-t,86400*int(days) if len(days) > 0 : if( (tnow -t) > 86400*int(days)): status = 'ignore '+filename+' for date' print status self.msg.config(text=unicode(status,'gbk'),font=('songti',9)) continue; # mt = time.localtime(t) size = os.path.getsize(fullname) # fileinfo = filename+','+time.strftime('%Y-%m-%d %H:%M:%S',mt)+','+str(size)+','+dirname+'n' print fileinfo file1.write(fileinfo) for dirname in dirs: self.listfile(dirname,file1,days,ext) if __name__=='__main__': #reload(sys) #sys.setdefaultencoding('utf8') root = Tk() root.title(unicode("搜尋工具-0.5- 周海漢 2008.12.16",'gbk')) app = capp(root) root.mainloop()