import os """ 文件名:myFindPath.py 主要功能: 1 创建目录树字典 2 遍历目录树字典 """ # 搜索文件目录树 class myPath_dict: def __init__(self, mpath): self.path_dict = {} self.path_dict = self._getDict(mpath) self.path = mpath self._display(self.path_dict,mpath) #创建目录树字典 def _getDict(self, mpath): # 获得指定目录中的内容 filelist = os.listdir(mpath) paths = {} for file_name in filelist: new_path = os.path.join(mpath, file_name) if os.path.isdir(new_path): paths[file_name] = self._getDict(new_path) return paths #遍历目录树字典 def _display(self, dict, mpath): for temp in dict.keys(): #print(temp) new_path = os.path.join(mpath, temp) print(new_path) new_dict = dict[temp] self._display(new_dict, new_path) #下面是程序入口 #mydict = myPath_dict("E:/2022")
学习python日记
最新推荐文章于 2024-09-28 19:04:49 发布