Python3.2完成简单文件搜索的代码,python3.2搜索代码,这是依赖python 3


这是依赖python 3.2来完成的简单文件搜索的方法。希望大牛们能帮我再改进下代码。

程序中我用到了python os\time、python re、pythonthreading多线程模块。

import osimport timeimport reimport threadingclass brid:    def __init__(self):        th=threading.Thread(target=self.dt(),args="")        th.start()        threading.Thread.join(th)        pass    def dt(self):        a=True        while a is True:            print("xx")            time.sleep(3)            a=False    def FileSearch(self,keywords,path):        print("searching...")        results=[]        i=0        j=0        time_start=time.time()        for root,dirs,filenames in os.walk(path):            for file in filenames:                i=i+1                if re.search(keywords,file):                    j=j+1                    filef=os.path.join(root,file)                    print(filef)                    results.append(filef)        time_end=time.time()        time_used=time_end-time_start        print("符合的文件 : ",j)        print("共扫描文件 : ",i)        print("花费时间 : ",time_used)        return results    def FileSearchEx(self):        keywords=input("the keywords : ")        path=input("target dir : ")        destination=input("the results : ")        print("searching...")        results=[]        i=0        j=0        time_start=time.time()        for root,dirs,filenames in os.walk(path):            for file in filenames:                i=i+1                if re.search(keywords,file):                    j=j+1                    filef=os.path.join(root,file)                    results.append(filef)        time_end=time.time()        time_used=time_end-time_start        fh=open(destination,"w+")        for t in results:            fh.write("\n"+t)        fh.write("\n符合的文件 : "+str(j))        fh.write("\n共扫描文件 : "+str(i))        fh.write("\n花费时间 : "+str(time_used))        fh.close()        os.system(destination)        return results        #www.iplaypy.comif __name__=="__main__":    yz=brid()    yz.FileSearchEx()            

编橙之家文章,

评论关闭