[英]File generator to get files from leaf folders ignoring hidden folders
我在最下面的文件夹中有一个包含一些 epub 和 json 文件的文件夹结构(不包括.ts
文件夹)。 我通过使用其他 json 文件创建一个.ts
文件夹,将标签从 json 文件导出到标签空间。 我已经处理了部分文件,现在我想在路径中没有.ts
文件夹的叶子文件夹中找到 json 文件,这样我就不必处理相同的文件两次。
我想在找到它们时处理目录中的文件,而不是获取所有文件的列表然后循环遍历它们。 这就是为什么我想做一个发电机。
在这个例子中,我应该得到文件test/t1/t2/test.json
作为结果,但我得到的是test/t1/test.json
。 这是错误的,因为t1
不是叶子文件夹。
test
├── t1
│ ├── t2
│ │ └── test.json
│ ├── test.json
│ └── .ts
│ └── test.json
└── .ts
└── t3
└── test.json
这是我尝试过的:
def file_generator(path: str) -> List[str]:
for root, subdirs, filenames in os.walk(path):
# If only hidden folders left, ignore current folder
if all([d[0] == '.' for d in subdirs]):
continue
# Ignore hidden subfolders
subdirs[:] = [d for d in subdirs if d[0] != '.']
# Return files in current folder
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',)
finally:
shutil.rmtree('test')
所以你颠倒了条件:你只跳过叶子文件夹,而不是其他任何东西。 而且您在错误的时间跳过,因为如果您不在叶子文件夹中,您仍然希望删除所有隐藏文件夹。
from typing import Iterator
# You don't actually return a list, so I changed it so it typechecks!
def file_generator(path: str) -> Iterator[str]:
for root, subdirs, filenames in os.walk(path):
# Ignore hidden subfolders
subdirs[:] = [d for d in subdirs if d[0] != '.']
# If any subfolders are left, ignore current folder
if subdirs:
continue
# Yield files in current folder
for filename in filenames:
if filename.endswith('.json'):
meta_file = os.path.join(root, filename)
yield meta_file
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.