开发文件生成器:筛选不含.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
- 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 subfoldert2) because there was no check for remaining non-hidden subdirectories. - Incorrect handling of directories with only hidden subfolders: The line
if all([d[0] == '.' for d in subdirs]): continuewas skipping valid leaf folders that only had hidden subdirectories (like.ts), which should be processed. - Missing check for paths containing
.ts: While you filtered out hidden subdirs, you didn't skip entire directory trees that are inside a.tsfolder.
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
- Skip
.tsdirectory trees: The checkif '.ts' in os.path.normpath(root).split(os.sep)ensures we don't process any files inside or under a.tsfolder, avoiding duplicate work. - Filter hidden subdirs: We modify
subdirs[:]to remove hidden folders, soos.walkwon't traverse into them (like.ts). - 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
相关产品推荐
相关产品推荐

