File size: 628 Bytes
256a159 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
import fnmatch
import os
from typing import List, Union
def match_files(path: str,
pattern: Union[str, List],
fuzzy: bool = False) -> List:
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])
|