我需要扫描文件在给定的文件夹/目录,如果递归是真的递归扫描目录,否则只有文件可以产生。我在下面代码实现,我得到None
import re
import uuid
from pathlib import Path
from typing import Generator, Tuple, Union, Optional
import time
def scan_files(dir_path: Union[str, Path], filter_regex: Optional[str] = None, recursive: bool = True) -> Generator[Tuple[str, Path], None, None]:
"""
Get list of files in the specified directory.
"""
path = Path(dir_path)
for item in path.iterdir():
if not item.is_symlink():
if recursive and item.is_dir():
yield from scan_files(item, filter_regex=filter_regex)
elif filter_regex is None or re.match(filter_regex, item.name, re.IGNORECASE):
yield str(uuid.uuid5(uuid.NAMESPACE_URL, str(item))), item
while True:
print("=" * 100)
for x, y in scan_files(dir_path="/tmp/in/106/", recursive=True):
print(x, y)
print("=" * 100)
time.sleep(4)
实际产量:scan_files不产生任何文件
测试输入文件
创建目录
/tmp/in/106/nested
add below files
/tmp/in/106/1.mov
/tmp/in/106/2.mov
/tmp/in/106/nested/1.mov
/tmp/in/106/nested/2.mov
预期产出:
if recursive: all files with full path
if non recursive: /tmp/in/106/1.mov and /tmp/in/106/2.mov
1条答案
按热度按时间gj3fmq9x1#
使用
Path.glob
和递归或非递归模式:编辑
使用一个catch-all正则表达式来更简洁地在一个函数中完成这一点: