根据关键字查找硬盘上的文件

根据关键字查找硬盘上的文件



[Copy to clipboard] [ - ]
CODE:
# -*- coding:gbk -*-
# file name findmyfile.py

import sys,os,fnmatch
def deepthfind(rootdir,writefile,patterns=''):
        endline = "\n";
        if writefile.endswith("html") or writefile.endswith("htm"):
             endline = "<br>";
        file_object = open(writefile, 'w')
        print "*** Searching ... "
       
        for path, subdirs, files in os.walk(rootdir):
            iswritepath = 1
            for name in files:
                fullname = path + os.sep +name
                if name.find(patterns)!=-1:
                        if iswritepath:
                                file_object.write("<b>"+path +"</b>"+ endline)
                                iswritepath = 0
                        file_object.write(name + endline)
        print "*** Done ! "
        file_object.close()


if len(sys.argv) < 2:
   print  "Usage : thisfile dir keywords"
if len(sys.argv) == 2:
   deepthfind(sys.argv[1])
if len(sys.argv) == 3:
   deepthfind(sys.argv[1],sys.argv[2])
if len(sys.argv) == 4:
   deepthfind(sys.argv[1],sys.argv[2],sys.argv[3])

example:
c:\>findmyfile.py  D:\\书籍  c:\\record.html  .chm
==============
2006-04-18 改版    谢谢wolfg !

加上[code]标签吧