繁体   English   中英

查找不是隐藏文件夹的叶子文件夹

[英]Find leaf folders that aren't hidden folders

我在最下面的文件夹中有一个包含一些 epub 和 json 文件的文件夹结构(不包括.ts文件夹)。 我通过使用其他一些json文件创建一个.ts文件夹,将标签从 json 文件导出到标签空间。 我已经处理了部分文件,现在我想找到路径中没有.ts文件夹的叶子文件夹,以找到剩余的文件,而不必处理其他文件两次。

所以对于这个例子,我只想为文件夹t5做一些事情:

test
├── t1
│   ├── t2
│   │   └── t5
│   └── t3
│       └── .ts
└── .ts
    └── t4

这是我尝试过的:

def process_files_in_leaf_subdirectories(dir: str) -> None:
    dirs = []
    for root, subdirs, filenames in os.walk(dir):
        if subdirs or '.ts' in root:
            continue
        dirs.append(root)
    return dirs


def test_process_files_in_leaf_subdirectories():
    os.makedirs('tmp/t1/t2/t5', exist_ok=True)
    os.makedirs('tmp/t1/t3/.ts', exist_ok=True)
    os.makedirs('tmp/.ts/t4', exist_ok=True)
    assert get_files_in_leaf_subdirectories('tmp') == ['tmp/t1/t2/t5']
    shutil.rmtree('tmp')

语境

由于您想找到叶子目录,而不计算.ts目录 - 只需递归访问非隐藏路径并生成没有任何子目录的目录就足够了。

对于 python 中的此类路径操作,我建议改用pathlib.Path

这是生成没有任何子目录的叶目录的生成器:

import pathlib

def find_leaf_dir_gen(root_path: pathlib.Path) -> pathlib.Path:

    # filter subdirectories
    child_dirs = [path for path in root_path.iterdir() if path.is_dir()]

    # if no child_dir, yield & return
    if not child_dirs:
        yield root_path
        return
    
    # otherwise iter tru subdir
    for path in child_dirs:
        # ignore hidden dir
        if path.stem[0] == ".":
            continue

        # step in and recursive yield
        yield from find_leaf_dir_gen(path)

示例使用

>>> leaves = list(find_leaf_dir_gen(ROOT))
>>> leaves
[WindowsPath('X:/test/t1/t2/t5'), WindowsPath('X:/test/t1/t3/t6')]

>>> for path in leaves:
...     ts_path = path.joinpath(".ts")
...     ts_path.mkdir()

测试目录结构 - 之前:

X:\TEST
├─.ts
│  └─t4
└─t1
    ├─t2
    │  └─t5
    └─t3
        ├─.ts
        └─t6

后:

X:\TEST
├─.ts
│  └─t4
└─t1
    ├─t2
    │  └─t5
    │      └─.ts
    └─t3
        ├─.ts
        └─t6
            └─.ts

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM