这就是我的目录结构:
<ROOT>/
├── root.log
├── a/
│ ├── xyz/
│ ├── a_xyz.log
├── b/
│ ├── xyz/
│ └── b_xyz.log
├── c/
│ ├── xyz/
│ └── d/
│ ├── xyz/
│ └── b_xyz.log
这是我正在使用的代码:
dirs_where_there_are_logs = {
os.path.relpath(os.path.dirname(pth), ROOT)
for pth in glob.iglob(f"{ROOT}/**/*.log", recursive=True)
}
这是输出,我得到:
{'.', 'a', 'b', 'c', 'c/d'}
但我不希望
glob
在搜索中包含目录,而只包含子目录。这就是我需要的输出:
{'a', 'b', 'c', 'c/d'}
现在,现在我可以添加
if
条件来忽略 '.'
输出,但我想知道是否有更多的 pythonic 可以用来实现这一点。
有的话请告诉我。
使用
root_dir
参数指定搜索的根目录:
glob.iglob("**/*.log", root_dir=ROOT, recursive=True)