You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

开发文件生成器:筛选不含.ts路径的叶子文件夹JSON文件(代码错误排查)

Fixing the File Generator for Leaf Folder JSON Files

Let's break down what went wrong in your original code and fix it to meet your requirements:

Key Issues in the Original Code

  1. It didn't check if the current directory is a leaf folder: Your code was yielding JSON files from non-leaf directories like test/t1 (which has a subfolder t2) because there was no check for remaining non-hidden subdirectories.
  2. Incorrect handling of directories with only hidden subfolders: The line if all([d[0] == '.' for d in subdirs]): continue was skipping valid leaf folders that only had hidden subdirectories (like .ts), which should be processed.
  3. Missing check for paths containing .ts: While you filtered out hidden subdirs, you didn't skip entire directory trees that are inside a .ts folder.

Corrected Code

import os
from typing import List
from pathlib import Path
import shutil

def file_generator(path: str) -> List[str]:
    for root, subdirs, filenames in os.walk(path):
        # Skip any directory (and its subdirectories) that lives inside a .ts folder
        if '.ts' in os.path.normpath(root).split(os.sep):
            continue
        
        # Remove hidden subdirectories from os.walk's traversal list
        subdirs[:] = [d for d in subdirs if not d.startswith('.')]
        
        # Only process files if this is a leaf folder (no non-hidden subdirectories left)
        if len(subdirs) == 0:
            for filename in filenames:
                if filename.endswith('.json'):
                    meta_file = os.path.join(root, filename)
                    yield meta_file

def test_file_generator():
    try:
        os.makedirs('test/t1/t2', exist_ok=True)
        os.makedirs('test/t1/.ts', exist_ok=True)
        os.makedirs('test/.ts/t3', exist_ok=True)
        Path('test/t1/t2/test.json').touch()
        Path('test/t1/test.json').touch()
        Path('test/t1/.ts/test.json').touch()
        Path('test/.ts/t3/test.json').touch()
        gen = file_generator('test')
        assert tuple(gen) == ('test/t1/t2/test.json',)
        print("Test passed!")
    finally:
        shutil.rmtree('test')

if __name__ == "__main__":
    test_file_generator()

Explanation of Changes

  1. Skip .ts directory trees: The check if '.ts' in os.path.normpath(root).split(os.sep) ensures we don't process any files inside or under a .ts folder, avoiding duplicate work.
  2. Filter hidden subdirs: We modify subdirs[:] to remove hidden folders, so os.walk won't traverse into them (like .ts).
  3. Leaf folder check: We only process files in directories where len(subdirs) == 0 (after filtering hidden subdirs). This ensures we only target the deepest non-hidden folders.

When you run the test function now, it will correctly return only test/t1/t2/test.json as expected.

内容的提问来源于stack exchange,提问作者user19924240

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.27 14:57:41