• [ python应用 ] python递归搜索文件,支持搜索多个文件,支持自定义处理动作


    写了一个PyQT界面的版本:https://github.com/LongchuanYu/pyqt_project

    PATH = r'E:MyDocument新しいフォルダー'  # 要搜索的目录地址
    
    TARGET_PATH = r'E:MyDocument新しいフォルダー1' # 把搜索结果复制到目的地址
    
    NAME_PATH = r'1.txt' # 要读取的文件(同时搜索多个)
    
    NAME = '333' # 搜索一个
    
    NAMES = [] # 自定义搜索,如果填写了这个字段,则忽略从文件加载
    
    READ_FROM_FILE = 1 #是否开启从文件加载
    
    
    
    
    
    import os
    from shutil import copyfile
    class Search():
        def __init__(self):
            self.names = []
            self.func = None
            self.once = True
            self.founded = False
        def __core(self,path,name):
            
            files = os.listdir(path)
            for file in files:
                if os.path.isdir(os.path.join(path,file)):
                    self.__core( os.path.join( path,file),name)
                elif name in file:
                    self.func(os.path.join(path,file))
                    self.founded = True
                    if self.once:return
        def searchName(self,path,name,func,once=True):
            self.func = func
            self.once = once
            self.founded = False
            self.__core(path,name)
            if not self.founded:
                print(name + ' 未找到')
        def searchNames(self,path,names,func,once=True):
            if not len(names):
                print("names error.")
                return
            
            self.names = names
            self.func = func
            self.once = once
            not_found_list = []
            for name in names:
                self.founded = False
                self.__core(path,name)
                if not self.founded:
                    not_found_list.append(name)
            if len(not_found_list):
                print('以下项目未找到:')
                for item in not_found_list:
                    print(item)
    
    
    def func(path):
        name = path.split('\')
        tar = os.path.join(TARGET_PATH,name[-1])
        copyfile(path,tar)
    
    def main():
        s = Search()
        # s.searchName(PATH,NAME,func,False)
        if not len(NAMES) and READ_FROM_FILE:
            with open(NAME_PATH,'r',encoding='utf-8') as f:
                for line in f:
                    NAMES.append(line.strip('
    '))
    
        s.searchNames(PATH,NAMES,func,False)
    main()
  • 相关阅读:
    从服务器角度分析RPG游戏——NPC的AI
    羽翼特效设计
    坐骑特效设计(二)
    坐骑特效设计
    Unity AssetBundle打包资源工具
    有趣的进度条
    原生与组件
    bower
    yeoman
    grunt+bower+yo
  • 原文地址:https://www.cnblogs.com/remly/p/12738411.html
Copyright © 2020-2023  润新知