简体   繁体   English

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

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

I have a folder structure with some epubs and json files in the down-most folders (not counting the .ts folders).我在最下面的文件夹中有一个包含一些 epub 和 json 文件的文件夹结构(不包括.ts文件夹)。 I'm exporting tags from the json files to tagspaces, by creating a .ts folder with some other json files.我通过使用其他一些json文件创建一个.ts文件夹,将标签从 json 文件导出到标签空间。 I've already processed part of the files and now I want to find the leaf folders that don't have a .ts folder in their path, to find the remaining files without having to process the others twice.我已经处理了部分文件,现在我想找到路径中没有.ts文件夹的叶子文件夹,以找到剩余的文件,而不必处理其他文件两次。

So for this example I only want to do something for the folder t5 :所以对于这个例子,我只想为文件夹t5做一些事情:

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

This is what I've tried:这是我尝试过的:

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')

context 语境

Since you want to find leaf directory, without counting .ts directory - just recursively visiting non-hidden path and yielding directories without any subdirectory would be enough.由于您想找到叶子目录,而不计算.ts目录 - 只需递归访问非隐藏路径并生成没有任何子目录的目录就足够了。

For such path operations in python, I'd recommend using pathlib.Path instead.对于 python 中的此类路径操作,我建议改用pathlib.Path

Here's generator to yield leaf directories without any subdir:这是生成没有任何子目录的叶目录的生成器:

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)

Sample usage示例使用

>>> 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()

Test directory structure - Before:测试目录结构 - 之前:

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

After:后:

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