Python遍历文件夹, 找出最深文件夹

dir_path = r"xxx"
ret = os.walk(dir_path)
dir_dict = {}

def dir_walk(dir):
    for ele_dir in dir:
        if ele_dir[1]:
            for i in ele_dir[1]:
                dir_name = os.path.join(ele_dir[0], i)
                dir_walk(os.walk(dir_name))
        else:
            tmp_list = ele_dir[0].split("\\")
            dir_dict[len(tmp_list)] = ele_dir[0]

dir_walk(ret)
for k in sorted(dir_dict)[::-1]:
    print(k, "\t", dir_dict[k])

del dir_dict
posted @ 2021-03-01 14:51  道友请留步W  阅读(510)  评论(0编辑  收藏  举报