OpenCompass/opencompass/utils/file.py
dmitrysarov cce5b6fbb6
fix output typing, change mutable list to immutable tuple (#989)
* fix output typing, change mutable list to immutable tuple

* import missed type

* format

---------

Co-authored-by: Leymore <zfz-960727@163.com>
2024-04-26 23:07:34 +08:00

22 lines
652 B
Python

import fnmatch
import os
from typing import List, Tuple, Union
def match_files(path: str,
pattern: Union[str, List],
fuzzy: bool = False) -> List[Tuple[str, str]]:
if isinstance(pattern, str):
pattern = [pattern]
if fuzzy:
pattern = [f'*{p}*' for p in pattern]
files_list = []
for root, _, files in os.walk(path):
for name in files:
for p in pattern:
if fnmatch.fnmatch(name.lower(), p.lower()):
files_list.append((name[:-3], os.path.join(root, name)))
break
return sorted(files_list, key=lambda x: x[0])