从根路径查找空目录和子目录

问题描述 投票:0回答:1

给定路径,提供所有空目录和子目录的列表。

我找到了许多仅列出目录(子目录)或仅列出给定路径下的文件的答案。 我需要所有空目录和子目录的列表才能删除它们。

python-3.x operating-system filesystems
1个回答
0
投票

这是我自己能实现的最好成绩:

import os
def findEmptyDir(path, empty_dir_array):
    for sdir in os.scandir(path):
        if sdir.is_dir():
            files = [f for f in os.scandir(sdir.path) if f.is_file()]
            if len(files) == 0:
                empty_dir_array.append({"dirpath" : sdir.path})
            findEmptyDir(sdir.path, empty_dir_array)
    return empty_dir_array


empty_dir_array = []
empty_dir_array = findEmptyDir("C:\\test", empty_dir_array)
print("Empty Dir List:", empty_dir_array)
© www.soinside.com 2019 - 2024. All rights reserved.