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

标签 python glob

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

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

这是代码:

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

返回:

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)

关于python - 检查 glob 中的路径是否为空?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51655212/

相关文章:

python - 当匹配不起作用时如何在主字符串的开头查找子字符串

python - 原子 : Can't search for Packages or Themes in the Install Packages section of Settings

python - 如何在 Flask-Login 中实现 user_loader 回调

python - 在后端处理 HTTP 和 websocket 连接

Python Selenium PhantomJS quit() 错误

shell - ./*/是可移植的吗?

python - 否定 fnmatch 模式

unix - Unix glob 模式中 dir/**/* 和 dir/*/* 之间的区别?

code-coverage - 将 karma 测试覆盖路径引入 Travis CI 的正确方法是什么?

python - 我不明白 Python 中的 'from'