检查 glob 中的路径是否为空?

Check if path is empty in glob?

检查 glob 搜索中是否有任何文件夹为空并打印它们的名称。

目录中有些文件夹是空的。

这是代码:

for i in glob.iglob('**/Desktop/testna/**' ,recursive = True):
    if not os.listdir(i): 
        print(f'{i} is empty' + '\n')

Returns:

NotADirectoryError

使用 os.walk:

可以更轻松地查找空目录
import os
print([root for root, dirs, files in os.walk('/') if not files and not dirs])

或者如果您更喜欢逐行打印文件:

import os
for root, dirs, files in os.walk('/'):
    if not files and not dirs:
        print(root)