python写的本地搜索小工具0.9

 
  1. #coding=utf-8
  2. #small search tool by tkinter
  3. #test plat form: Windows XP Chinese
  4. #version 0.9
  5. #author: zhouhh
  6. #file name:find.py
  7. #date: 2008.12.16
  8. #email:ablozhou # gmail.com
  9. #note:to search files modified a few days ago
  10. #debug tool: PythonWin
  11. #python version: 2.5.2
  12. #py2exe:
  13. #create a setup.py:
  14. ###########################
  15. ## setup.py
  16. #from distutils.core import setup
  17. #import py2exe
  18. #setup(console=["find.py"])
  19. #
  20. ##########################
  21. #runing cmd: python setup.py py2exe
  22. import os;
  23. import time;
  24. from Tkinter import *
  25. class capp:
  26.     ''''' 对输入的路径和文件后缀以及编辑的天数进行搜索,并在输入路径下生成一个csv结果文件。'''
  27.     def __init__(self,master):
  28.         ''''' file search tool 0.9 by [email protected] 周海汉 '''
  29.         frame1 = Frame(master)
  30.         frame1.pack(padx=5)
  31.         self.quest = Label(frame1,bitmap='questhead')
  32.         self.quest.pack(side=LEFT)
  33.         self.dirlable=Label(frame1,text=unicode('输入搜索路径: ','utf8'),font=('songti',9))
  34.         self.dirlable.pack(side=LEFT)
  35.         self.entry = Entry(frame1,font=('songti',9),width=65)
  36.         self.entry.pack(side=LEFT)
  37.         frame = Frame(master)
  38.         frame.pack(pady=5)
  39.         self.lblext=Label(frame,text=unicode('      文件后缀名: ','utf8'),font=('songti',9))
  40.         self.lblext.pack(side=LEFT)
  41.         
  42.         self.ext = Entry(frame,width=15)
  43.         self.ext.pack(side=LEFT,padx=5)
  44.         self.extstr=StringVar()
  45.         self.extstr.set(u'*.xls')
  46.         self.ext['textvariable']=self.extstr
  47.         self.contents = StringVar()
  48.         self.contents.set(u'')
  49.         self.entry['textvariable']=self.contents
  50.         self.daylable=Label(frame,text=unicode('   修改时间(几天前):','utf8'),font=('songti',9))
  51.         self.daylable.pack(side=LEFT)        
  52.         self.days = Entry(frame,width=20)
  53.         self.days.pack(side=LEFT,padx=5)
  54.         self.daystr=StringVar()
  55.         self.daystr.set('1')
  56.         self.days['textvariable']=self.daystr 
  57.         
  58.         frame2 = Frame(master)
  59.         frame2.pack(pady=5)
  60.         self.resultlabel = Label(frame2,text=unicode('结果保存路径','utf8'),font=('songti',9))
  61.         self.resultlabel.pack(side=LEFT,padx=8)
  62.         self.resultfilestr = StringVar()
  63.         self.resultfile = Entry(frame2,font=('songti',9),width=40)
  64.         self.resultfile.pack(side=LEFT)
  65.         self.resultfile['textvariable']=self.resultfilestr
  66.         self.resultfilestr.set(u'')
  67.         self.hello =  Button(frame2,text=unicode('查找','utf8'),font=('songti',12,'bold'),fg='red',height=1,width=8,command=self.sayhi)
  68.         self.hello.bind('',self.rolloverEnter)
  69.         self.hello.bind('',self.rolloverLeave)
  70.         self.hello.pack(side=LEFT,padx=5)
  71.       #  self.button = Button(frame2,text=unicode('退出','utf8'),font=('songti',12),height=1,command=master.quit)
  72.       #  self.button.bind('',self.rolloverEnter)
  73.       #  self.button.bind('',self.rolloverLeave)
  74.       #  self.button.pack(side=LEFT,padx=5)
  75.         frame3 = Frame(master)
  76.         frame3.pack(pady=5)
  77.         self.msg = Label(frame3,text=unicode('','utf8'))
  78.         self.msg.pack(side=LEFT)
  79.         
  80.     def sayhi(self):
  81.         print 'begining ... '
  82.         self.msg.config(text='...')
  83.         try:
  84.             mydir = self.contents.get()
  85.             if len(mydir) <= 0:
  86.                 mydir = os.getcwd()
  87.                 self.contents.set(unicode(mydir,'utf8'))
  88.             os.chdir(mydir)
  89.             resultdir = self.resultfilestr.get()
  90.             if len(resultdir) <=0:
  91.                 resultdir = mydir            
  92.                 self.resultfilestr.set(resultdir)
  93.             mydays = self.daystr.get()
  94.             myext =self.extstr.get()
  95.             file1 = open(os.path.join(resultdir,'result.csv'),'a+')
  96.             searchinfo = '/nsearch '+myext+' in '+mydays+' days. Time:'+time.strftime('%Y-%m-%d %H:%M:%S',time.localtime(time.time()))+' /n'
  97.             file1.write(searchinfo)
  98.             file1.write('File name, Modify time, Size (bytes), Directory/n'
  99.             self.listfile(mydir,file1,mydays,myext)
  100.             file1.close()
  101.             os.chdir(mydir)# change path to the begining path
  102.             self.msg.config(text=unicode('搜索成功!请查看'+os.path.join(resultdir.encode('utf8'),'result.csv'),'utf8'),font=('songti',9))        
  103.         except IOError,(errno,strerror):
  104.             print 'IO error %s: %s' % (errno,strerror)
  105.             self.msg.config(text = 'IO error %s: %s' % (errno,strerror))
  106.             os.chdir(mydir)
  107.         except:
  108.             print "Unexpected error:", sys.exc_info()[0]
  109.             self.msg.config(text='error '+str(sys.exc_info()[0]))
  110.             file1.close()
  111.             os.chdir(mydir)
  112.             raise
  113.    # def pressedPlain(self):
  114.    #     showinfo("Message","You pressed Plain Button")
  115.     def rolloverEnter(self,event):
  116.         event.widget.config(relief=GROOVE)
  117.     def rolloverLeave(self,event):
  118.         event.widget.config(relief=RAISED)
  119.         
  120.     def listfile(self,dirname,file1,days,ext):
  121.         ''''' search files ... '''
  122.         if len(ext) >0 :
  123.             ext = os.path.splitext(ext)[1]
  124.         if len(dirname) > 0:
  125.             os.chdir(dirname)
  126.         dirname = os.getcwd()
  127.         print '['+dirname+']:'
  128.         names = os.listdir(dirname)
  129.         
  130.         dirs=[]
  131.         for filename in names:
  132.             fullname = os.path.join(dirname,filename)
  133.             if  os.path.isdir(fullname):
  134.                 dirs.append(fullname)
  135.                 continue
  136.             if len(ext) > 0:
  137.                 if os.path.splitext(filename)[1]!=ext :
  138.                     status = 'ignore '+filename+' for extension'
  139.                     print status
  140.                     self.msg.config(text=status,font=('songti',9))
  141.                     continue;
  142.                 
  143.             t = os.path.getmtime(fullname)
  144.             tnow = time.time()
  145.             #print fullname,' ignore',tnow,t,tnow-t,86400*int(days)
  146.             if len(days) > 0 :
  147.                 if( (tnow -t) > 86400*int(days)):
  148.                     status = 'ignore '+filename+' for date'
  149.                     print status
  150.                     self.msg.config(text=status,font=('songti',9))
  151.                     continue;
  152.             mt = time.localtime(t)
  153.             size = os.path.getsize(fullname)
  154.             fileinfo = filename+','+time.strftime('%Y-%m-%d %H:%M:%S',mt)+','+str(size)+','+dirname+'/n'
  155.             print fileinfo
  156.             file1.write(fileinfo)
  157.     
  158.         for dirname in dirs:
  159.             self.listfile(dirname,file1,days,ext)
  160.             
  161. if __name__=='__main__':
  162.     #reload(sys)
  163.     #sys.setdefaultencoding('utf8')
  164.     root = Tk()
  165.     root.title(unicode("搜索工具-0.9- 周海汉 2008.12.17",'utf8'))
  166.     app = capp(root)
  167.     root.mainloop()
  168.     
  169.         

你可能感兴趣的:(网络编程,编程语言)