学习python日记

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")
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值