搜索路径,,# encoding:


# encoding: utf-8import osclass SearchPath(object):    @classmethod    def get(cls, search_path, key_word, search_type='file'):        '''指定路径搜索,多个关键字用list'''        if not os.path.exists(search_path):            print 'no this path'            return []        type_dict = {'file': 2, 'dir': 1}        result = []        for info in os.walk(search_path):            for name in info[type_dict[search_type]]:                if isinstance(key_word, str):                    if key_word in name:                        result.append(info[0] + '/' + name)                if isinstance(key_word, list):                    if all([n in name for n in key_word]):                        result.append(info[0] + '/' + name)        return result       if __name__ == '__main__':    # 在/Users/jimi/desktop/books路径下搜索名字中包含’dive‘的文件    print SearchPath.get('/Users/jimi/desktop/books', 'dive')    # 在/Users/jimi/desktop/books路径下搜索名字中包含’pl‘和‘al’的文件    print SearchPath.get('/Users/jimi/desktop/books', ['pl', 'al'])    # 搜索文件夹    SearchPath.get('/Users/jimi/desktop/books', 'stage', search_type='dir')

评论关闭