import os
class SearchFile():
def __init__(self,directory,endswith):
self.directory = directory
self.endswith = endswith
def search_files(self):
# 将路径处理为系统兼容
dire = os.path.normpath(self.directory)
objects = {}
# 当前目录 子目录们 文件们
for curdir, subdirs, files in os.walk(dire):
for file in files:
if file.endswith(self.endswith):
# 当前目录名 作为类别标签
label = curdir.split(os.path.sep)[-1]
if label not in objects:
objects[label] = []
# 把path加入到label中
path = os.path.join(curdir, file)
objects[label].append(path)
return objects
if __name__ == "__main__":
s = SearchFile('directory','.py')
print(s.search_files())
工具(三)--Python获取指定目录下指定文件格式的文件路径小工具
最新推荐文章于 2024-09-05 07:38:48 发布