zoukankan      html  css  js  c++  java
  • [ 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()
  • 相关阅读:
    JQ 选择器大全
    .NET SOCKET通信编程
    .Net中的Socket通讯
    SQL Server 2008中新增的变更数据捕获(CDC)和更改跟踪
    C# 编写Window服务基础(一)
    更改windows服务的配置文件app.config
    SQLServer数据库表中将指定列分组转一行
    在c#中使用mongo-csharp-driver操作mongodb
    java 线程池的原理
    Java并发编程:ThreadLocal的使用以及实现原理解析
  • 原文地址:https://www.cnblogs.com/remly/p/12738411.html
Copyright © 2011-2022 走看看